-
Notifications
You must be signed in to change notification settings - Fork 4
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
b49c736
commit 90966f0
Showing
2 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/ui/docs-bundle/src/pages/api/fern-docs/auth/api-key-injection.ts
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
42 changes: 42 additions & 0 deletions
42
packages/ui/docs-bundle/src/pages/api/fern-docs/auth/jwt/callback.ts
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,42 @@ | ||
import { verifyFernJWTConfig } from "@/server/auth/FernJWT"; | ||
import { getAuthEdgeConfig } from "@/server/auth/getAuthEdgeConfig"; | ||
import { withSecureCookie } from "@/server/auth/withSecure"; | ||
import { getXFernHostEdge } from "@/server/xfernhost/edge"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export const runtime = "edge"; | ||
|
||
function redirectWithLoginError(location: string, errorMessage: string): NextResponse { | ||
const url = new URL(location); | ||
url.searchParams.set("loginError", errorMessage); | ||
return NextResponse.redirect(url.toString()); | ||
} | ||
|
||
export default async function handler(req: NextRequest): Promise<NextResponse> { | ||
if (req.method !== "GET") { | ||
return new NextResponse(null, { status: 405 }); | ||
} | ||
|
||
const domain = getXFernHostEdge(req); | ||
const edgeConfig = await getAuthEdgeConfig(domain); | ||
|
||
const token = req.nextUrl.searchParams.get("fern_token"); | ||
const state = req.nextUrl.searchParams.get("state"); | ||
const redirectLocation = state ?? `https://${domain}/`; | ||
|
||
if (edgeConfig?.type !== "basic_token_verification" || token == null) { | ||
return redirectWithLoginError(redirectLocation, "Couldn't login, please try again"); | ||
} | ||
|
||
try { | ||
await verifyFernJWTConfig(token, edgeConfig); | ||
|
||
const res = NextResponse.redirect(redirectLocation); | ||
res.cookies.set("fern_token", token, withSecureCookie()); | ||
return res; | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
return redirectWithLoginError(redirectLocation, "Couldn't login, please try again"); | ||
} | ||
} |