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,62 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const code = url.searchParams.get("code");
|
||||
const state = url.searchParams.get("state");
|
||||
const oauthError = url.searchParams.get("error");
|
||||
|
||||
const cookieStore = await cookies();
|
||||
const storedState = cookieStore.get("oauth_state")?.value;
|
||||
const verifier = cookieStore.get("oauth_verifier")?.value;
|
||||
|
||||
const fail = (reason: string) => {
|
||||
const response = NextResponse.redirect(
|
||||
new URL(`/oauth/error?reason=${reason}`, url.origin)
|
||||
);
|
||||
response.cookies.delete("oauth_verifier");
|
||||
response.cookies.delete("oauth_state");
|
||||
return response;
|
||||
};
|
||||
|
||||
if (oauthError || !code || !state || !verifier) return fail("auth_failed");
|
||||
if (state !== storedState) return fail("state_mismatch");
|
||||
|
||||
const tokenResponse = await fetch(process.env.KOTOSOFT_ID_TOKEN_URL!, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
grant_type: "authorization_code",
|
||||
code,
|
||||
client_id: process.env.CLIENT_ID!,
|
||||
client_secret: process.env.CLIENT_SECRET!,
|
||||
redirect_uri: process.env.REDIRECT_URI!,
|
||||
code_verifier: verifier,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!tokenResponse.ok) return fail("token_exchange_failed");
|
||||
const { access_token } = await tokenResponse.json();
|
||||
|
||||
const sessionResponse = await fetch(`${process.env.BACKEND_URL}/api/auth/session`, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${access_token}` },
|
||||
});
|
||||
if (!sessionResponse.ok) return fail("session_failed");
|
||||
|
||||
const { session_token } = await sessionResponse.json();
|
||||
|
||||
const response = NextResponse.redirect(new URL("/collections", url.origin));
|
||||
|
||||
response.cookies.set("session", session_token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: 60 * 60 * 24 * 30,
|
||||
});
|
||||
response.cookies.delete("oauth_verifier");
|
||||
response.cookies.delete("oauth_state");
|
||||
return response;
|
||||
}
|
||||
Reference in New Issue
Block a user