2024-12-12 07:23:46 +00:00
|
|
|
'use server';
|
|
|
|
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
|
|
import { getKeycloakIdpToken, getPatreonMemberships } from "@/app/lib/patreon";
|
|
|
|
import { syncronizeKeycloakRoles } from '@/app/lib/keycloak';
|
|
|
|
import { authOptions } from "@/app/lib/auth";
|
|
|
|
|
|
|
|
export async function GET(req: Request, res: Response) {
|
|
|
|
|
2024-12-16 20:39:23 +00:00
|
|
|
// return NextResponse.json(['test'])
|
|
|
|
|
2024-12-12 07:23:46 +00:00
|
|
|
console.log('lets run getServerSession()')
|
|
|
|
|
|
|
|
|
|
|
|
// @TODO @TODO @TODO @todo @todo @todo THE PROBLEM IS WITH getServerSession()!!!!
|
|
|
|
// or rather, the JWE we generated in middleware.ts is invalid and the error is manifesting within the getServerSession() invocation.
|
|
|
|
const session = await getServerSession(authOptions);
|
|
|
|
// console.log('session as follows')
|
|
|
|
// console.log(session)
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
|
2024-12-16 20:39:23 +00:00
|
|
|
// console.log('currently-entitled-tiers lets get user id')
|
|
|
|
// console.log(session)
|
|
|
|
|
|
|
|
|
2024-12-12 07:23:46 +00:00
|
|
|
let keycloakIdpToken, patreonTiersList
|
|
|
|
|
|
|
|
if (!session.token?.access_token) {
|
|
|
|
console.error('session.token.access_token was missing')
|
|
|
|
return NextResponse.json({ error: `Failed to get access token from Session`}, { status: 500 })
|
|
|
|
}
|
|
|
|
|
2024-12-16 20:39:23 +00:00
|
|
|
if (!session.token.sub) {
|
|
|
|
return NextResponse.json({ error: `failed to get profile.sub from Session` }, { status: 400 })
|
|
|
|
}
|
|
|
|
|
2024-12-12 07:23:46 +00:00
|
|
|
try {
|
|
|
|
keycloakIdpToken = await getKeycloakIdpToken(session.token.access_token)
|
|
|
|
} catch (e) {
|
2024-12-16 20:39:23 +00:00
|
|
|
console.error(e)
|
2024-12-12 07:23:46 +00:00
|
|
|
return NextResponse.json({ error: `Failed to get Patreon token (Keycloak IDP). e=${e}`}, { status: 401 })
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
patreonTiersList = await getPatreonMemberships(keycloakIdpToken)
|
|
|
|
} catch (e) {
|
2024-12-16 20:39:23 +00:00
|
|
|
console.error(e)
|
2024-12-12 07:23:46 +00:00
|
|
|
return NextResponse.json({ error: `Failed to get patreon memberships. e=${e}`}, { status: 401 })
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// side effect which grants the appropriate keycloak roles to the user
|
|
|
|
try {
|
2024-12-16 20:39:23 +00:00
|
|
|
await syncronizeKeycloakRoles(session.token.sub, patreonTiersList)
|
2024-12-12 07:23:46 +00:00
|
|
|
} catch (e) {
|
2024-12-16 20:39:23 +00:00
|
|
|
console.error(e)
|
2024-12-12 07:23:46 +00:00
|
|
|
return NextResponse.json({ error: `Failed to syncronize roles` }, { status: 500 })
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NextResponse.json(patreonTiersList);
|
|
|
|
}
|
|
|
|
return NextResponse.json({ error: "You must be logged in." }, { status: 401 });
|
|
|
|
}
|