在 remix-auth 中注册后如何存储用户会话?

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

我正在使用 https://github.com/sergiodxa/remix-auth-github

我想知道,如果我有一个注册屏幕,注册完成后存储用户会话的正确方法是什么,这样用户就不必再次登录?

为了更清楚地解释这一点。假设我有一个函数:

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

完成后,我想将用户的会话信息存储在 cookie 中,而不是让他们再次登录。执行此操作的最佳方法是什么?

这里的作者,如果您的身份验证器存储 return 由 signup 编辑的数据,那么您可以这样做:

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) },
  });
}

这样,现在当您调用 authenticator.isAuthenticated(request) 时它会工作并且 return 用户对象。