mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Implement authentication
This commit is contained in:
parent
cdcd71cf2a
commit
3e4551ea7a
23 changed files with 656 additions and 406 deletions
22
src/middleware.ts
Normal file
22
src/middleware.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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) {
|
||||
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