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

Add method to set user session on the client #199

Open
tobiasdiez opened this issue Oct 2, 2024 · 2 comments
Open

Add method to set user session on the client #199

tobiasdiez opened this issue Oct 2, 2024 · 2 comments

Comments

@tobiasdiez
Copy link

The playground contains the following logic to login a user:

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

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.

@atinux
Copy link
Owner

atinux commented Oct 4, 2024

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.

@tobiasdiez
Copy link
Author

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.

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

2 participants