fp/services/next/app/api/patreon/currently-entitled-tiers/route.ts

66 lines
2.0 KiB
TypeScript

'use server';
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { getPatreonMemberships } from "@/app/lib/patreon";
import { authOptions } from "@/app/lib/auth";
export async function GET(req: Request, res: Response) {
// return NextResponse.json(['test'])
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) {
// console.log('currently-entitled-tiers lets get user id')
// console.log(session)
let 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 })
}
if (!session.token.sub) {
return NextResponse.json({ error: `failed to get profile.sub from Session` }, { status: 400 })
}
// try {
// idpToken = await get(session.token.access_token)
// } catch (e) {
// console.error(e)
// return NextResponse.json({ error: `Failed to get Patreon token (Keycloak IDP). e=${e}`}, { status: 401 })
// }
try {
patreonTiersList = await getPatreonMemberships(idpToken)
} catch (e) {
console.error(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.token.sub, patreonTiersList)
} catch (e) {
console.error(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 });
}