nuxt-community / auth-module

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

watchState not called on change #156

Open nikkanetiya opened 6 years ago

nikkanetiya commented 6 years ago

What problem does this feature solve?

Hello,

Is there any ways to add a hook like a user logged in with social login then directly its data will be sent directly to store. What if I want to fetch the users data from backend server before setting it up to store.

What does the proposed changes look like?

Allow hook/callback in a loginWith promise to be used in social login.

This feature request is available on Nuxt.js community (#c115)
luiseok commented 6 years ago

I also need this feature.

phani1kumar commented 3 years ago

+1 What is the purpose of

this.$auth.$storage.watchState('loggedIn', (newValue) => {
      console.log('Logged in user:', newValue)
    })

This function never gets triggered no matter where I register this listener! I kept it in login.vue and also in default.vue layout file, but this function never gets triggered!

phani1kumar commented 3 years ago

By adding a plugin auth.js as below:

export default ({ app }) => {
  // Only _actual_ login/outs (including resets) will be watched here.
  console.log('Registering watch state in plugin!')
  app.$auth.$storage.watchState('user', (userNew, userOld) => {
    alert('User has signed in here')
    console.log('User signed in:', userNew, userOld)
    if (userNew) {
      // Follow @nuxtjs/auth workflow.
      app.$auth.redirect('home')
    }
  })
}

and auth config in nuxt.config.js as:

  auth: {
    watchLoggedIn: true,
    watchUser: true,
    plugins: [
      { src: '~/plugins/auth', mode: 'client' }
    ],
....
....

I am able to see the console.log getting printed during logout. But during login, nothing gets printed. Is this because of the social login redirects? Is there any way I can tap into the social login after ?