mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-12 07:56:40 +00:00
organize componenst a lib
This commit is contained in:
16
src/lib/auth/isValidUser.ts
Normal file
16
src/lib/auth/isValidUser.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { GetUserResponse } from "@server/routers/user";
|
||||
import { verifySession } from "./verifySession";
|
||||
|
||||
export async function isValidUser(): Promise<GetUserResponse | null> {
|
||||
const user = await verifySession();
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!user.emailVerified) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
23
src/lib/auth/verifySession.ts
Normal file
23
src/lib/auth/verifySession.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { internal } from "@app/api";
|
||||
import { GetUserResponse } from "@server/routers/user";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function verifySession(): Promise<GetUserResponse | null> {
|
||||
const sessionId = cookies().get("session")?.value ?? null;
|
||||
|
||||
try {
|
||||
const res = await internal.get<AxiosResponse<GetUserResponse>>(
|
||||
"/user",
|
||||
{
|
||||
headers: {
|
||||
Cookie: `session=${sessionId}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return res.data.data;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user