Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware not working when accessing directly to a auth route #1

Open
DannyAndres opened this issue Aug 31, 2022 · 8 comments
Open

Comments

@DannyAndres
Copy link

If I access /home from url as the first page to load it will not find auth variable with

const auth = getAuth();
nuxtApp.vueApp.provide('auth', auth);
nuxtApp.provide('auth', auth);

because it cannot find it, it will not get passed to the middleware behaving as if there is no user at all.

When the user is already loaded everything work as expected, I suspect it is getAuth() which does not load as quick as it should causing all of this.

@minstn
Copy link

minstn commented Sep 9, 2022

i guess you mean /secret page not /home? in fact, the "logged in user" is only known after the onAuthStateChanged is called.

@DannyAndres
Copy link
Author

yeah, /secret and /home in this case is to represent a route which should be protected and only accesible with a logged user. I think the problem comes with onAuthStateChanged takes some time to load at the beginning

@silvanm
Copy link

silvanm commented Oct 29, 2022

I think the problem comes with onAuthStateChanged takes some time to load at the beginning

I agree that this is the reason.

Is there a way to wait for onAuthStateChanged to be called on before the Middlewares are being called?

Or should we store the current user in localStorage so that we're independent of onAuthStateChanged?

@smoakey
Copy link

smoakey commented Mar 21, 2023

@silvanm did you ever find an answer to your problem above?

@Eronne
Copy link

Eronne commented Apr 10, 2023

Same issue here! Middleware are triggered before the user is authenticated...

@superdiazzz
Copy link

I have strange issue in my middleware, when i console log like below:

export default defineNuxtPlugin(() => {
    addRouteMiddleware('global-test', () => {

    }, { global: true })

    addRouteMiddleware('auth', async() => {

        const { $auth } = useNuxtApp()


        console.log("call auth middle 1", $auth);

        console.log('call auth middle 2', $auth.currentUser);  
    })
})

first console shows there is a value, but the second is null??

Screen Shot 2024-01-21 at 16 49 42

@minstn
Copy link

minstn commented Jan 24, 2024 via email

@EntertainmentPortal
Copy link

Found the solution:

  • Remove the plugins/auth.js
  • Create a folder called 'middleware'
  • Create a file inside middleware called 'auth.js'
  • Add this code to middleware/auth.js:
    export default defineNuxtRouteMiddleware((to, from) => { const user = useFirebaseUser(); const allowedRoutes = ["/", "/login"]; if (user.value) { if (allowedRoutes.includes(to.path)) { return navigateTo("/secret"); } } else { if (!allowedRoutes.includes(to.path)) { return navigateTo("/"); } } });
  • Update the allowed routes accordingly to your page structure
  • Remove the 'middleware: ['auth']' from definePageMeta on every page.
  • That's it.

What it does: it defines the middleware globally, guarding all routes. If you want any exceptions, you can add them to the allowed list. It checks for the useFirebaseUser, which contains a watcher too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants