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

66 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-12-12 07:23:46 +00:00
'use server';
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
2025-01-11 03:10:04 +00:00
import { getPatreonMemberships } from "@/app/lib/patreon";
2024-12-12 07:23:46 +00:00
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)
2025-01-11 03:10:04 +00:00
let patreonTiersList
2024-12-12 07:23:46 +00:00
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 })
}
2025-01-11 03:10:04 +00:00
// 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 })
// }
2024-12-12 07:23:46 +00:00
try {
2025-01-11 03:10:04 +00:00
patreonTiersList = await getPatreonMemberships(idpToken)
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 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 });
}