nuxt-auth-utils icon indicating copy to clipboard operation
nuxt-auth-utils copied to clipboard

Add method to set user session on the client

Open tobiasdiez opened this issue 1 year ago • 2 comments

The playground contains the following logic to login a user: https://github.com/atinux/nuxt-auth-utils/blob/e0396255b2ebc6b3ef89b9dd62f0eb0fbf345389/playground/components/AuthLogin.vue#L10-L17

This triggers two requests: one for the login, the other one to get the session data. If there were a method setSession or loggedIn then one could return the session information as part of the login response.

tobiasdiez avatar Oct 02 '24 10:10 tobiasdiez

This would mean to add hooks to $fetch that can bring side-effects actually, I rather keep it separated ad this applied only for email/password kind of login.

atinux avatar Oct 04 '24 11:10 atinux

What I had in mind was to add a method

setSessionState(value) {
   useSessionState().value = value
   const authReadyState = useAuthReadyState()
   if (!authReadyState.value) {
    authReadyState.value = true
  }
}

that then can be used in the login flow as follows:

await $fetch('/api/login', { 
     method: 'POST', 
     body: { 
       email: target.email.value, 
       password: target.password.value, 
     }, 
   }).then((value) => { 
      setSessionState(value)
     // previously this was fetch() 
   })

And perhaps expose the server-side helper that currently sets the fetch return, so that one can use this to return the same value in the login api.

tobiasdiez avatar Oct 05 '24 07:10 tobiasdiez