version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies, headers as fetchHeaders } from "next/headers";
|
||||
import { generateCodeVerifier, generateCodeChallenge, generateState } from "@/lib/pkce";
|
||||
|
||||
const FLOW_TTL = 60 * 10;
|
||||
|
||||
export async function GET() {
|
||||
if ((await cookies()).get("session")) {
|
||||
const headers = await fetchHeaders();
|
||||
const protocol = headers.get("x-forwarded-proto") ?? "http";
|
||||
const host = headers.get("host");
|
||||
const origin = host ? `${protocol}://${host}` : "http://localhost:3000";
|
||||
return NextResponse.redirect(new URL("/", origin));
|
||||
}
|
||||
|
||||
const verifier = generateCodeVerifier();
|
||||
const challenge = generateCodeChallenge(verifier);
|
||||
const state = generateState();
|
||||
|
||||
const authorizeUrl = new URL(process.env.KOTOSOFT_ID_AUTHORIZE_URL!);
|
||||
authorizeUrl.searchParams.set("response_type", "code");
|
||||
authorizeUrl.searchParams.set("client_id", process.env.CLIENT_ID!);
|
||||
authorizeUrl.searchParams.set("redirect_uri", process.env.REDIRECT_URI!);
|
||||
authorizeUrl.searchParams.set("scope", "profile:read");
|
||||
authorizeUrl.searchParams.set("code_challenge", challenge);
|
||||
authorizeUrl.searchParams.set("code_challenge_method", "S256");
|
||||
authorizeUrl.searchParams.set("state", state);
|
||||
|
||||
const response = NextResponse.redirect(authorizeUrl);
|
||||
const cookieOpts = {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax" as const,
|
||||
maxAge: FLOW_TTL,
|
||||
path: "/",
|
||||
};
|
||||
|
||||
response.cookies.set("oauth_verifier", verifier, cookieOpts);
|
||||
response.cookies.set("oauth_state", state, cookieOpts);
|
||||
|
||||
return response;
|
||||
}
|
||||
Reference in New Issue
Block a user