login to next backend

This commit is contained in:
2026-04-18 23:45:46 +01:00
parent 2131a34e33
commit 174febe986
7 changed files with 139 additions and 80 deletions
+33
View File
@@ -0,0 +1,33 @@
import { NextResponse } from "next/server";
export async function GET(req: Request) {
const url = new URL(req.url);
const code = url.searchParams.get("code");
// exchange code for token (Keycloak token endpoint)
const tokenRes = await fetch("https://keycloak.petermaquiran.xyz/realms/tvone/protocol/openid-connect/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: "tvone-web",
client_secret: "7jQUciQCCf2WRFRe170UANKzGKVWFIkY",
grant_type: "authorization_code",
code: code!,
redirect_uri: "http://localhost:3000/api/auth/callback",
}),
});
const text = await tokenRes.text();
var data = JSON.parse(text);
const res = NextResponse.redirect("http://localhost:3000/dashboard");
res.cookies.set("access_token", data.access_token, {
httpOnly: true,
secure: true,
sameSite: "lax",
path: "/",
});
return res;
}
+14
View File
@@ -0,0 +1,14 @@
export async function GET() {
const redirect = encodeURIComponent(
"http://localhost:3000/api/auth/callback"
);
const keycloakUrl =
`https://keycloak.petermaquiran.xyz/realms/tvone/protocol/openid-connect/auth` +
`?client_id=tvone-web` +
`&response_type=code` +
`&scope=openid` +
`&redirect_uri=${redirect}`;
return Response.redirect(keycloakUrl);
}
+14
View File
@@ -0,0 +1,14 @@
export async function GET() {
const redirect = encodeURIComponent(
"http://localhost:3000/api/auth/callback"
);
const keycloakUrl =
`https://keycloak.petermaquiran.xyz/auth/realms/tvone/protocol/openid-connect/auth` +
`?client_id=tvone-web` +
`&response_type=code` +
`&scope=openid` +
`&redirect_uri=${redirect}`;
return Response.redirect(keycloakUrl);
}
+16
View File
@@ -0,0 +1,16 @@
import { NextResponse } from "next/server";
export async function POST(req: Request) {
const { token } = await req.json();
const res = NextResponse.json({ ok: true });
res.cookies.set("auth_token", token, {
httpOnly: true,
secure: true,
sameSite: "lax",
path: "/",
});
return res;
}