11import { initializeLucia , initializeGitHub } from "@/lib/auth"
22import { DB } from "@/lib/db"
3- import { defineMiddleware } from "astro:middleware"
3+ // import { defineMiddleware } from "astro:middleware"
4+ import { clerkMiddleware } from "@clerk/astro/server"
5+ import type { MiddlewareHandler } from "astro"
6+ import { sequence } from "astro:middleware"
47
5- // import { verifyRequestOrigin } from "lucia"
8+ const clerkAuth = clerkMiddleware ( )
69
7- export const onRequest = defineMiddleware ( async ( context , next ) => {
10+ const setupContext : MiddlewareHandler = ( context , next ) => {
811 context . locals . db = new DB ( context . locals . runtime . env . DB )
912 const lucia = initializeLucia ( context . locals . runtime . env . DB )
1013 context . locals . lucia = lucia
@@ -13,31 +16,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
1316 context . locals . runtime . env . GITHUB_CLIENT_SECRET ,
1417 )
1518 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
4219 return next ( )
43- } )
20+ }
21+
22+ export const onRequest = sequence ( clerkAuth , setupContext )
0 commit comments