N
Nuxt6mo ago
Gary

Simple Lambda Cookie Question

tldr: Do cookies work on initial page load on AWS Lambda? Hey folks - building something for aws-lambda and everything is up and running except the useCookie method on initial load: I'm reading it in some routeMiddleware and looks something like this:
export default defineNuxtRouteMiddleware((to) => {
if (to.path !== '/pin-code') {
const accessToDocs = useCookie('access-to-docs')

if (!accessToDocs.value || accessToDocs.value.toString() !== '123456')
return navigateTo(`/pin-code`, { redirectCode: 401 })
}
})
export default defineNuxtRouteMiddleware((to) => {
if (to.path !== '/pin-code') {
const accessToDocs = useCookie('access-to-docs')

if (!accessToDocs.value || accessToDocs.value.toString() !== '123456')
return navigateTo(`/pin-code`, { redirectCode: 401 })
}
})
So, simple test to see if they are on any route other than pin-code and if they are I read the cookie. If the cookie isnt there or it doesn't equal to my super secure password I redirect them to the pin-code page. This works totally fine locally. On Lambda it works on any subsequent page as you click around and router is used. However, initial page load doesn't seem to be able to read the cookie. What am I missing?
2 Replies
xibman
xibman6mo ago
@Gary try to check when you're middleware is executed : https://nuxt.com/docs/guide/directory-structure/middleware#when-middleware-runs
Nuxt
middleware/ · Nuxt Directory Structure
Nuxt provides middleware to run code before navigating to a particular route.
xibman
xibman6mo ago
set it as global to
Want results from more Discord servers?
Add your server