nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 924 forks source link

The loggedin is still true after loggout called #1745

Open Frank-Z20 opened 2 years ago

Frank-Z20 commented 2 years ago

I can loggin successfully and the loggedin is true, but when i loggout the loggedin is still true. This only happened on server. If I run npm dev it has no problem.

tommytob commented 1 year ago

We have the same problem over here, I made a small hack that works perfectly.

Create a small route middleware file and place this direct after the auth middleware in the nuxt.config.js

{
  route: {
     middleware: [
       'auth', 'auth-validation'
     ]
  },
  ...
}
// middleware/auth-validation.js
export default async function ({ $auth, route }) {
    // current token is invalid but the user is still loggedIn
    if(!$auth.strategy.token.status().valid() && 
       !$auth.strategy.refreshToken.status().valid() && 
       $auth.loggedIn
    ) {
        // do not remove loggedIn when we authenticate
        if(!["login", "callback"].includes(route.name)) {
            $auth.setUser(false);
        }
    }
}