File size: 598 Bytes
fc69895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62a1333
fc69895
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Elysia from "elysia";
import { authenticateRequest } from "../auth";
import { config } from "../config";

export const authPlugin = new Elysia({ name: "auth" }).derive(
	{ as: "scoped" },
	async ({
		headers,
		cookie,
		request,
	}): Promise<{
		locals: App.Locals;
	}> => {
		const auth = await authenticateRequest(
			{ type: "elysia", value: headers },
			{ type: "elysia", value: cookie },
			new URL(request.url, config.PUBLIC_ORIGIN || undefined),
			true
		);
		return {
			locals: {
				user: auth?.user,
				sessionId: auth?.sessionId,
				isAdmin: auth?.isAdmin,
			},
		};
	}
);