mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Better-auth configuration
This commit is contained in:
parent
a02da83781
commit
5d04693a09
9 changed files with 62 additions and 144 deletions
|
|
@ -1,22 +1,18 @@
|
|||
import { auth } from 'auth'
|
||||
import { defineMiddleware } from 'astro:middleware'
|
||||
import { setSessionTokenCookie, deleteSessionTokenCookie, validateSessionToken, COOKIE_NAME } from 'utils/session'
|
||||
|
||||
export const onRequest = defineMiddleware(async (context, next) => {
|
||||
const token = context.cookies.get(COOKIE_NAME)?.value
|
||||
if (!token) {
|
||||
const isAuthed = await auth.api.getSession({
|
||||
headers: context.request.headers
|
||||
})
|
||||
|
||||
if (isAuthed) {
|
||||
context.locals.user = isAuthed.user
|
||||
context.locals.session = isAuthed.session
|
||||
} else {
|
||||
context.locals.user = null
|
||||
context.locals.session = null
|
||||
return next()
|
||||
}
|
||||
|
||||
const { session, user } = await validateSessionToken(token)
|
||||
if (session !== null) {
|
||||
setSessionTokenCookie(context.cookies, token, session.expiresAt)
|
||||
} else {
|
||||
deleteSessionTokenCookie(context.cookies)
|
||||
}
|
||||
|
||||
context.locals.session = session
|
||||
context.locals.user = user
|
||||
return next()
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue