mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-19 19:36:38 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { internal } from "@app/lib/api";
|
|
import { authCookieHeader } from "@app/lib/api/cookies";
|
|
import { GetUserResponse } from "@server/routers/user";
|
|
import { AxiosResponse } from "axios";
|
|
import { pullEnv } from "../pullEnv";
|
|
import { cache } from "react";
|
|
|
|
export const verifySession = cache(async function ({
|
|
skipCheckVerifyEmail,
|
|
forceLogin
|
|
}: {
|
|
skipCheckVerifyEmail?: boolean;
|
|
forceLogin?: boolean;
|
|
} = {}): Promise<GetUserResponse | null> {
|
|
const env = pullEnv();
|
|
|
|
try {
|
|
const search = new URLSearchParams();
|
|
if (forceLogin) {
|
|
search.set("forceLogin", "true");
|
|
}
|
|
const res = await internal.get<AxiosResponse<GetUserResponse>>(
|
|
`/user?${search.toString()}`,
|
|
await authCookieHeader()
|
|
);
|
|
|
|
const user = res.data.data;
|
|
|
|
if (!user) {
|
|
return null;
|
|
}
|
|
|
|
if (
|
|
!skipCheckVerifyEmail &&
|
|
!user.emailVerified &&
|
|
env.flags.emailVerificationRequired
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return user;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
});
|