Home > Mobile >  How to store user session after signup in remix-auth?
How to store user session after signup in remix-auth?

Time:04-18

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?

CodePudding user response:

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.

  • Related