-
Notifications
You must be signed in to change notification settings - Fork 14
Getting auth middleware working on pages #29
Comments
If globalMiddleware is true you just have to set |
Ah ok that makes sense. But where do I set auth: false when inside setup? Previously it would be done as follows <script>
export default {
auth: 'guest'
};
</script> But this does not work inside Also just to clarify, is this the correct place to set globalMiddleware auth: {
globalMiddleware: true,
strategies: {...},
} |
definePageMeta({
auth: 'guest',
}); |
Hmmm, I had tried this earlier before trying I get the same error as originally posted. It seems that as soon as I add Currently I have removed all definePageMeta and I have also created a completely stripped down version of my app that just has 2 pages with no logic and just these 3 modules to reduce chance of other conflicts
When I have |
I think for the moment I'm going to disable global middleware or remove the middleware (again) as it seems that nuxt3 has multiple variable changes that I keep having to account for. See #23 (comment) |
Okay, I have a workaround for now using custom middleware that checks the store using getStore you added today to see if a user is logged in. Here is it in case it is of use to anyone. Save file in middleware/auth.js with export default function auth() {
const ctx = useNuxtApp();
const auth = ctx.$auth.$storage.getStore();
if (auth.user === null || auth.user === false) {
return navigateTo('/auth/login');
}
} Then it can be setup on any page using definePageMeta definePageMeta({
middleware: ['auth']
}); |
sometimes I get an error like this: Cannot read properties of undefined (reading '$storage') |
Is it when you're using globalMiddleware? |
when I use the solution from andyjamesn if I use the property
I get an error |
updated nuxt to rc2, I'll see if there are more such errors, maybe it was fixed in nuxt/framework#4645 |
It seems to me that the error disappeared after updating Nuxt. I will test further. |
Fyi, there is an issue on rc2, currently fixed only on the main branch. |
In what way would it affect it? Are you able to provide examples? |
closing this as it seems rc2 has solved the global middleware issue. |
I am trying to get auth middleware working on pages to automatically route to the login page if user is not logged in
Using:
I do have globalMiddleware set to true my auth setup in nuxt config
globalMiddleware: true
But I am getting an error:
ERROR [worker] nuxt instance unavailable
at Module.useNuxtApp (/Users/Andy/Personal%20Projects/FlowRocket/ui-kit-repos/app/.nuxt/dist/server/server.mjs:818:13)
at Timeout._onTimeout (/Users/Andy/Personal%20Projects/FlowRocket/ui-kit-repos/app/.nuxt/dist/server/server.mjs:3825:39)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
Is there a way to get this working?
The text was updated successfully, but these errors were encountered: