我正在使用https://github.com/sergiodxa/remix-auth-github。
我想知道,如果我有一個注冊螢屏,注冊完成后存盤用戶會話的正確方法是什么,這樣用戶就不必再次登錄?
為了更清楚地解釋這一點。想象一下我有一個功能:
async function signup(userInfo) {
await DB.insertUser(userInfo)
}
完成后,我想將用戶的會話資訊存盤在 cookie 中,而不是讓他們再次登錄。最好的方法是什么?
uj5u.com熱心網友回復:
作者在這里,如果您的 Authenticator 存盤了回傳的資料,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)它時,它將作業并回傳用戶物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460636.html
上一篇:無密碼短信驗證-令牌過期和安全
