Auth Strategy does not persist in local storage or cookies (and user gets logged out) on reloading the page

connecteev
0
connecteev
commented 6 months ago

Version

v4.8.4

Reproduction link

http://www.nourl.com

Steps to reproduce

I have 2 auth strategies set up in nuxt.config.js.

Renaming 'local' to 'local2' or 'social_login' refuses to keep the user logged-in on refreshing the page.

This is what I do before triggering the login.
this.$auth.setStrategy('social_login');

But after I am logged in, if I refresh the page, I do not stay logged in. Looking at the local storage and cookies, it seems that the auth.strategy flips back to local. Looks like a bug to me.

Here's my code:

nuxt.config.js:

strategies: {
      // For login with email and password
      local: {
        endpoints: {
          login: {
            url: `${backendApiUrl}/api/v1/auth/login`,
            method: "post",
            propertyName: 'access_token', // the name of the token object that comes back from this API request
          },
          user: {
            url: `${backendApiUrl}/api/v1/auth/loggedinUser`,
            method: 'get',
            propertyName: 'user', // the name of the user object that comes back from this API request
          },
          logout: {
            url: `${backendApiUrl}/api/v1/auth/logout`,
            method: 'post',
          },
        },
      },

      // For Social Auth using Laravel Socialite
      social_login: {
        _scheme: "local",
        endpoints: {
          login: {
            url: "auth/login",
            method: "post",
            propertyName: 'token', // the name of the token object that comes back from this API request
          },
          user: {
            url: "auth/me",
            method: "get",
            propertyName: 'data', // the name of the user object that comes back from this API request
          },
          logout: {
            url: "auth/logout",
            method: "get"
          }
        }
      },

My social login code looks like this:

socialLogin.vue:

<template>
    <div class="social-login">
        <button @click="socialLogin('google')" class="social-login-btn google">
            <i class="fab fa-google"></i>
        </button>
        <button @click="socialLogin('github')" class="social-login-btn github">
            <i class="fab fa-github"></i>
        </button>
        <button @click="socialLogin('facebook')" class="social-login-btn facebook">
            <i class="fab fa-facebook"></i>
        </button>
        <button @click="socialLogin('twitter')" class="social-login-btn twitter">
            <i class="fab fa-twitter"></i>
        </button>
        <button @click="socialLogin('linkedin')" class="social-login-btn linkedin">
            <i class="fab fa-linkedin"></i>
        </button>
    </div>
</template>

<script>
export default {
    methods: {
        socialLogin(service) {
            this.$auth.setStrategy('social_login');
            window.location.href = process.env.backendApiUrl + `/api/v1/auth/login/${service}`;
        }
    }
}
</script>

social-callback.vue:

<template>
    <div class="tw-container">
        <div class="loader"></div>
        <p>Please wait while we're logging you in...</p>
    </div>
</template>

<script>
export default {
    middleware: ["guest", "hideTestRoutes"],
    data() {
        return {
            token: this.$route.query.token ? this.$route.query.token : null
        }
    },
    mounted() {
        this.$auth.setToken('social_login', 'Bearer ' + this.token);
        this.$auth.setStrategy('social_login');

        this.$auth.fetchUser().then( () => {
            return this.$router.push('/');
        }).catch( (e) => {
            this.$auth.logout();
            return this.$router.push(`/auth/${this.$route.query.origin ? this.$route.query.origin : 'register'}?error=1`);
        });
    }
}
</script>

What is expected ?

User should STAY logged-in on refresh

What is actually happening?

The user does not stay logged-in on refresh

bug
0
Informations
Bug Report โ€ข Open
#c431 - Created 6 months ago