mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-03-30 14:25:37 +00:00
✨ feat: initial prototype
This commit is contained in:
44
apps/backend/src/lib/auth-plugin.ts
Normal file
44
apps/backend/src/lib/auth-plugin.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { isUserSuspended } from "@minikura/db";
|
||||
import { Elysia } from "elysia";
|
||||
import { auth } from "./auth";
|
||||
|
||||
async function getSessionFromHeaders(headers: Headers | Record<string, string>) {
|
||||
const headersObj =
|
||||
headers instanceof Headers ? headers : new Headers(headers as Record<string, string>);
|
||||
|
||||
return auth.api.getSession({
|
||||
headers: headersObj,
|
||||
});
|
||||
}
|
||||
|
||||
export const authPlugin = new Elysia({ name: "auth" })
|
||||
.mount(auth.handler)
|
||||
.derive({ as: "scoped" }, async ({ request }) => {
|
||||
const session = await getSessionFromHeaders(request.headers);
|
||||
|
||||
if (
|
||||
session?.user &&
|
||||
isUserSuspended(
|
||||
session.user as unknown as Pick<
|
||||
{ isSuspended: boolean; suspendedUntil: Date | null },
|
||||
"isSuspended" | "suspendedUntil"
|
||||
>
|
||||
)
|
||||
) {
|
||||
return {
|
||||
user: null,
|
||||
session: null,
|
||||
isAuthenticated: false,
|
||||
isSuspended: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
user: session?.user || null,
|
||||
session: session?.session || null,
|
||||
isAuthenticated: Boolean(session?.user),
|
||||
isSuspended: false,
|
||||
};
|
||||
});
|
||||
|
||||
export type AuthPlugin = typeof authPlugin;
|
||||
Reference in New Issue
Block a user