mirror of
https://github.com/PeterMaquiran/tvone.git
synced 2026-04-22 20:15:51 +00:00
34 lines
746 B
TypeScript
34 lines
746 B
TypeScript
|
|
import { cookies } from "next/headers";
|
||
|
|
|
||
|
|
export type UserProfile = {
|
||
|
|
id: string;
|
||
|
|
email?: string;
|
||
|
|
name?: string;
|
||
|
|
username?: string;
|
||
|
|
roles: string[];
|
||
|
|
picture?: string
|
||
|
|
};
|
||
|
|
|
||
|
|
export async function getUserProfile(): Promise<UserProfile | null> {
|
||
|
|
const cookieStore = cookies();
|
||
|
|
const token = (await cookieStore).get("access_token")?.value;
|
||
|
|
|
||
|
|
if (!token) return null;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const payload = JSON.parse(
|
||
|
|
Buffer.from(token.split(".")[1], "base64").toString()
|
||
|
|
);
|
||
|
|
|
||
|
|
return {
|
||
|
|
id: payload.sub,
|
||
|
|
email: payload.email,
|
||
|
|
name: payload.name,
|
||
|
|
username: payload.preferred_username,
|
||
|
|
picture: payload.picture,
|
||
|
|
roles: payload.realm_access?.roles || [],
|
||
|
|
};
|
||
|
|
} catch (err) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|