11import { initializeLucia , initializeGitHub } from "@/lib/auth"
22import { DB } from "@/lib/db"
3- import { defineMiddleware } from "astro:middleware"
3+ import { clerkMiddleware } from "@clerk/astro/server"
4+ import type { MiddlewareHandler } from "astro"
5+ import { sequence } from "astro:middleware"
46
5- // import { verifyRequestOrigin } from "lucia"
7+ const clerkAuth = clerkMiddleware ( )
68
7- export const onRequest = defineMiddleware ( async ( context , next ) => {
9+ const setupContext : MiddlewareHandler = ( context , next ) => {
810 context . locals . db = new DB ( context . locals . runtime . env . DB )
911 const lucia = initializeLucia ( context . locals . runtime . env . DB )
1012 context . locals . lucia = lucia
@@ -13,31 +15,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
1315 context . locals . runtime . env . GITHUB_CLIENT_SECRET ,
1416 )
1517 context . locals . github = github
16- const sessionId = context . cookies . get ( lucia . sessionCookieName ) ?. value ?? null
17- if ( ! sessionId ) {
18- context . locals . user = null
19- context . locals . session = null
20- return next ( )
21- }
22-
23- const { session, user } = await lucia . validateSession ( sessionId )
24- if ( session && session . fresh ) {
25- const sessionCookie = lucia . createSessionCookie ( session . id )
26- context . cookies . set (
27- sessionCookie . name ,
28- sessionCookie . value ,
29- sessionCookie . attributes ,
30- )
31- }
32- if ( ! session ) {
33- const sessionCookie = lucia . createBlankSessionCookie ( )
34- context . cookies . set (
35- sessionCookie . name ,
36- sessionCookie . value ,
37- sessionCookie . attributes ,
38- )
39- }
40- context . locals . session = session
41- context . locals . user = user
4218 return next ( )
43- } )
19+ }
20+
21+ export const onRequest = sequence ( clerkAuth , setupContext )
0 commit comments