I use this middleware:
export const onRequest = defineMiddleware(async (context, next) => {
if (!isProtected(context.url.pathname)) {
return next()
}
const token = context.cookies.get(TOKEN)?.value
const validationResult = await verifyAuth(
token,
context.url.pathname.split('/')[2],
)
switch (validationResult.status) {
case 'authorized':
const jwtVerifyResult = await jwtVerify(token, secret)
const { name } = jwtVerifyResult.payload
console.log('middleware: ', name)
context.locals.player = name
return next()
I access it in an astro file like this:
const nameOfPlayer = Astro.locals.player
After I navigate to another route Astro.locals.player becomes undefined but I still need that name