PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 4, 2022

[FIXED] How to store user session after signup in remix-auth?

 September 04, 2022     authentication, remix.run     No comments   

Issue

I'm using https://github.com/sergiodxa/remix-auth-github.

I was wondering, if I have a signup screen, what is the correct method to store the user session once the signup is finished, so the user does not have to log in again?

To explain this more clearly. Imagine I have a function:

async function signup(userInfo) {
   await DB.insertUser(userInfo)
}

Once this is finished, I would like to store the user's session information inside of the cookie instead of having them log in again. What's the best method to do this?


Solution

Author here, if your Authenticator stores the data returned by signup then you could do something like this:

export let action: ActionFunction = async ({ request }) => {
  // get the user info from the formData, however you are doing it, this
  // depends on your app
  let userInfo = await getUserInfo(request)

  // register the user with your function
  let user = await signup(userInfo)

  // get the session object from the cookie header, the getSession should
  // be the same returned by the sessionStorage you pass to Authenticator
  let session = await getSession(request.headers.get("cookie"))

  // store the user in the session using the sessionKey of the
  // Authenticator, this will ensure the Authenticator isAuthenticated
  // method will be able to access it
  session.set(authenticator.sessionKey, user)

  // redirect the user somewhere else, the important part is the session
  // commit, you could also return a json response with this header
  return redirect("/somewhere", {
    headers: { "Set-Cookie": await commitSession(session) },
  });
}

This way, now when you call authenticator.isAuthenticated(request) it will work and return the user object.



Answered By - Sergio Xalambrí
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing