Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 4, 2024
1 parent b49c736 commit 90966f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OAuth2Client } from "@/server/auth/OAuth2Client";
import { APIKeyInjectionConfig, getAPIKeyInjectionConfig } from "@/server/auth/getApiKeyInjectionConfig";
import { getAPIKeyInjectionConfig } from "@/server/auth/getApiKeyInjectionConfig";
import { getAuthEdgeConfig } from "@/server/auth/getAuthEdgeConfig";
import { withSecureCookie } from "@/server/auth/withSecure";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import { OryAccessTokenSchema } from "@fern-ui/ui/auth";
import { APIKeyInjectionConfig, OryAccessTokenSchema } from "@fern-ui/ui/auth";
import { NextRequest, NextResponse } from "next/server";
import { WebflowClient } from "webflow-api";
import type { OauthScope } from "webflow-api/api/types/OAuthScope";
Expand Down
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");
}
}

0 comments on commit 90966f0

Please sign in to comment.