Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Getting auth middleware working on pages #29

Closed
andyjamesn opened this issue Mar 28, 2022 · 14 comments
Closed

Getting auth middleware working on pages #29

andyjamesn opened this issue Mar 28, 2022 · 14 comments

Comments

@andyjamesn
Copy link

I am trying to get auth middleware working on pages to automatically route to the login page if user is not logged in

Using:

definePageMeta({
    middleware: ['auth']
});

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?

@Denoder
Copy link
Owner

Denoder commented Mar 28, 2022

If globalMiddleware is true you just have to set auth: false or auth: 'guest' (depending on your needs) what you are doing is calling the middleware twice since it's globally enabled and using middleware will call it again individually.

@andyjamesn
Copy link
Author

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 <script setup>

Also just to clarify, is this the correct place to set globalMiddleware

auth: {
      globalMiddleware: true,
      strategies: {...},
}

@Denoder
Copy link
Owner

Denoder commented Mar 28, 2022

definePageMeta({
    auth: 'guest',
});

@andyjamesn
Copy link
Author

andyjamesn commented Mar 28, 2022

Hmmm, I had tried this earlier before trying middleware: ['auth']

I get the same error as originally posted.

Error

It seems that as soon as I add globalMiddleware: true, I get this error.

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

modules: [
        '@nuxtjs-alt/auth',
        '@nuxtjs-alt/axios',
        '@nuxtjs-alt/pinia',
    ]

When I have globalMiddleware: true, I get the nuxt instance unavailable error

@Denoder
Copy link
Owner

Denoder commented Mar 28, 2022

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)

@andyjamesn
Copy link
Author

andyjamesn commented Mar 28, 2022

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']
});

@4KDA
Copy link

4KDA commented May 3, 2022

sometimes I get an error like this:

Cannot read properties of undefined (reading '$storage')

@Denoder
Copy link
Owner

Denoder commented May 3, 2022

Is it when you're using globalMiddleware?

@4KDA
Copy link

4KDA commented May 3, 2022

Is it when you're using globalMiddleware?

when I use the solution from andyjamesn
while not setting the globalMiddleware property

if I use the property
globalMiddleware: true,
with the variant

definePageMeta({
    auth: 'guest',
});

I get an error
vue-router.esm-bundler.js?v=fc261905:3302 TypeError: Cannot read properties of undefined (reading 'options')
at middleware.mjs:13:41

@4KDA
Copy link

4KDA commented May 3, 2022

updated nuxt to rc2, I'll see if there are more such errors, maybe it was fixed in nuxt/framework#4645

@4KDA
Copy link

4KDA commented May 3, 2022

It seems to me that the error disappeared after updating Nuxt. I will test further.

@emazyka
Copy link

emazyka commented May 9, 2022

Fyi, there is an issue on rc2, currently fixed only on the main branch.
I know it might not be related to this exact issue but it can affect the auth behaviour in general.
nuxt/framework#4770

@Denoder
Copy link
Owner

Denoder commented May 10, 2022

Fyi, there is an issue on rc2, currently fixed only on the main branch. I know it might not be related to this exact issue but it can affect the auth behaviour in general. nuxt/framework#4770

In what way would it affect it? Are you able to provide examples?

@Denoder
Copy link
Owner

Denoder commented May 10, 2022

closing this as it seems rc2 has solved the global middleware issue.
@emazyka if you have any issues related to your previous comment please create a new issue.

@Denoder Denoder closed this as completed May 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants