54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
|
'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) {
|
||
|
|
||
|
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) {
|
||
|
|
||
|
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 })
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
keycloakIdpToken = await getKeycloakIdpToken(session.token.access_token)
|
||
|
} catch (e) {
|
||
|
return NextResponse.json({ error: `Failed to get Patreon token (Keycloak IDP). e=${e}`}, { status: 401 })
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
patreonTiersList = await getPatreonMemberships(keycloakIdpToken)
|
||
|
} catch (e) {
|
||
|
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 {
|
||
|
await syncronizeKeycloakRoles(session.user.id, patreonTiersList)
|
||
|
} catch (e) {
|
||
|
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 });
|
||
|
}
|