This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdb7fb4
commit 2b4122e
Showing
7 changed files
with
71 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use client' | ||
|
||
import { Button } from '@/components/ui/button' | ||
import { useRouter } from 'next/navigation' | ||
|
||
interface Props { | ||
error: Error & { digest?: string } | ||
reset: () => void | ||
} | ||
|
||
export default function Error({ error }: Props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<div className="flex h-full w-full flex-1 flex-col items-center justify-center gap-3 py-12"> | ||
<p className="mt-8 text-2xl font-extrabold">Something Went Wrong!</p> | ||
<p className="mb-4 max-w-[36rem] text-lg font-semibold"> | ||
{error.message || 'Unknown Error'} | ||
</p> | ||
<Button variant="outline" onClick={router.refresh}> | ||
Reload | ||
</Button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,9 @@ | ||
import { ACCESS_TOKEN_EXPIRE_TIME, API_BASE_URL } from '@/lib/vars' | ||
import { encode, getToken } from 'next-auth/jwt' | ||
import { parseCookie } from 'next/dist/compiled/@edge-runtime/cookies' | ||
import { NextResponse, type NextRequest } from 'next/server' | ||
import { withAuth } from 'next-auth/middleware' | ||
|
||
const getAuthToken = (res: Response) => { | ||
const Authorization = res.headers.get('authorization') as string | ||
const parsedCookie = parseCookie(res.headers.get('set-cookie') || '') | ||
const refreshToken = parsedCookie.get('refresh_token') as string | ||
const refreshTokenExpires = parsedCookie.get('Expires') as string | ||
return { | ||
accessToken: Authorization, | ||
refreshToken, | ||
accessTokenExpires: Date.now() + ACCESS_TOKEN_EXPIRE_TIME - 30 * 1000, | ||
refreshTokenExpires: Date.parse(refreshTokenExpires) - 30 * 1000 | ||
export default withAuth({ | ||
pages: { | ||
signIn: '/login' | ||
} | ||
} | ||
}) | ||
|
||
const sessionCookieName = process.env.NEXTAUTH_URL?.startsWith('https://') | ||
? '__Secure-next-auth.session-token' | ||
: 'next-auth.session-token' | ||
|
||
export const middleware = async (req: NextRequest) => { | ||
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET }) | ||
if (req.nextUrl.pathname.startsWith('/console') && !token) | ||
return NextResponse.redirect(new URL('/', req.url)) | ||
|
||
if (token && token.accessTokenExpires <= Date.now()) { | ||
const reissueRes = await fetch(API_BASE_URL + '/auth/reissue', { | ||
headers: { | ||
cookie: `refresh_token=${token.refreshToken}` | ||
}, | ||
cache: 'no-store' | ||
}) | ||
if (reissueRes.ok) { | ||
const { | ||
accessToken, | ||
refreshToken, | ||
accessTokenExpires, | ||
refreshTokenExpires | ||
} = getAuthToken(reissueRes) | ||
const newToken = await encode({ | ||
secret: process.env.NEXTAUTH_SECRET as string, | ||
token: { | ||
...token, | ||
accessToken, | ||
refreshToken, | ||
accessTokenExpires, | ||
refreshTokenExpires | ||
}, | ||
maxAge: 24 * 60 * 60 // 24 hours | ||
}) | ||
req.cookies.set(sessionCookieName, newToken) | ||
const res = NextResponse.next({ | ||
request: { | ||
headers: req.headers | ||
} | ||
}) | ||
res.cookies.set(sessionCookieName, newToken, { | ||
maxAge: 24 * 60 * 60, | ||
secure: process.env.NODE_ENV === 'production', | ||
httpOnly: true, | ||
sameSite: 'lax' | ||
}) | ||
return res | ||
} else if (reissueRes.status == 401) { | ||
req.cookies.delete(sessionCookieName) | ||
const res = NextResponse.next({ | ||
request: { | ||
headers: req.headers | ||
} | ||
}) | ||
res.cookies.delete(sessionCookieName) | ||
return res | ||
} | ||
} | ||
return NextResponse.next({ | ||
request: { | ||
headers: req.headers | ||
} | ||
}) | ||
} | ||
export const config = { matcher: ['/console/:path*'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters