-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement saving token and automatically refreshing token
- Loading branch information
1 parent
7c075be
commit d83d492
Showing
10 changed files
with
305 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare namespace NodeJS { | ||
interface ProcessEnv { | ||
NEXTAUTH_SECRET?: string; | ||
NEXTAUTH_URL?: string; | ||
NEXT_PUBLIC_API_BASE_URL?: string; | ||
SERVICE_API_BASE_URL?: string; | ||
} | ||
} |
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,28 @@ | ||
import { getAuthFetchHeaders } from "@/lib/server/auth"; | ||
|
||
import { type NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const fetchHeaders = await getAuthFetchHeaders(req); | ||
|
||
if (!fetchHeaders) { | ||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
const systemsRes = await fetch( | ||
`${process.env.SERVICE_API_BASE_URL}/radio/system/list`, | ||
{ | ||
method: "GET", | ||
headers: fetchHeaders, | ||
} | ||
); | ||
|
||
if (!systemsRes.ok) { | ||
return NextResponse.json( | ||
{ error: systemsRes.statusText }, | ||
{ status: systemsRes.status } | ||
); | ||
} | ||
|
||
return NextResponse.json(await systemsRes.json()); | ||
} |
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,53 @@ | ||
import { getServerSession as nextAuthGetServerSession } from "next-auth"; | ||
import { OPTIONS } from "@/config/nextAuthOptions"; | ||
import { getToken } from "next-auth/jwt"; | ||
import { parseRefreshToken } from "@/utils/fetchUtils"; | ||
|
||
import type { | ||
GetServerSidePropsContext, | ||
NextApiRequest, | ||
NextApiResponse, | ||
} from "next"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
export async function getServerSession( | ||
...args: | ||
| [GetServerSidePropsContext["req"], GetServerSidePropsContext["res"]] | ||
| [NextApiRequest, NextApiResponse] | ||
| [] | ||
) { | ||
return await nextAuthGetServerSession(...args, OPTIONS); | ||
} | ||
|
||
export async function getServerJWT( | ||
req: GetServerSidePropsContext["req"] | NextRequest | NextApiRequest | ||
) { | ||
return await getToken({ req, secret: process.env.JWT_SECRET }); | ||
} | ||
|
||
export type AuthFetchHeaders = | ||
| { Authorization: string } | ||
| { Authorization: string; Cookie: string } | ||
| undefined; | ||
|
||
export async function getAuthFetchHeaders( | ||
req: GetServerSidePropsContext["req"] | NextRequest | NextApiRequest | ||
): Promise<AuthFetchHeaders> { | ||
const session = await getServerSession(); | ||
const token = await getServerJWT(req); | ||
|
||
if (!session || !token) { | ||
return undefined; | ||
} | ||
|
||
const refreshTokenCookie = parseRefreshToken(token.apiCookies); | ||
|
||
return refreshTokenCookie | ||
? { | ||
Authorization: `Bearer ${session.user.accessToken}`, | ||
Cookie: refreshTokenCookie, | ||
} | ||
: { | ||
Authorization: `Bearer ${token.accessToken}`, | ||
}; | ||
} |
Oops, something went wrong.