简体   繁体   English

网站身份验证并添加到期时间

[英]Authentication for website and adding an expiry

I'm changing the authentication to handle expiry on a website that is still under dev.我正在更改身份验证以处理仍在开发中的网站上的到期。 (so have full control over changes at the moment). (因此现在可以完全控制更改)。 This is what I currently have (Pseudo code).这就是我目前拥有的(伪代码)。 I have included 4 routes and the db currently holds userToken (id of user), userSecret (changes when user logs out)我已经包含了 4 条路由,数据库当前包含 userToken(用户的 id)、userSecret(用户注销时的更改)

SignUpRoute.post('/signup', (req, res) => {
//Save userdetails, userToken, userSecret in DB
}


BidderRoute.post('/logout', userAuth, (req, res) => {
find account with matching email
update db.userSecret
response success
}

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)  
response (db.userToken, db.userToken)  //can this be a cookie?
    
ProductRoute.post('/data', userAuth, (req, res) => {
// some action here
}


userAuth middleware:
req.body.userSecret, req.body.userToken from body
fetch db collection where req.body.userToken === db.userToken
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
next()

I require to add an expiry to prevent the user from being logged in for more than 30mins, so figured I should add a expiry field in collection and alter /login route and middleware:我需要添加一个过期时间以防止用户登录超过 30 分钟,所以我想我应该在集合中添加一个过期字段并更改 /login 路由和中间件:

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)    
updated and save db.userSecret
updated and save db.expiryDate //30mins
response (db.userToken, db.userToken)  //can this be a cookie?

ProductRoute.post('/data', userAuth, (req, res) => {
// some action here
}
    
userAuth middleware:
req.body.userSecret, req.body.userToken from body
fetch db collection where req.body.userToken === db.userToken
if  now > db.expiryDate  then error (401) - redirect to login
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
if db.expiryDate < 2mins remaining then renew db.expiryDate (save in db)
next()

Q1. Q1。 Before I implement I'm wondering if I've missed anything obvious in the steps.在我实施之前,我想知道我是否遗漏了步骤中的任何明显内容。

Q2. Q2。 Currently the front-end stores the userSecret and User token in local storage, and the server sends out a 200 response with userToken and userSecret (not a cookie) (see /login).目前,前端将 userSecret 和 User 令牌存储在本地存储中,服务器发送带有 userToken 和 userSecret(不是 cookie)的 200 响应(参见 /login)。 If I want to hold this data in a cookie on the FE, should the above code be sending a cookie instead or does it not matter as the FE can save the response asa cookie?如果我想将这些数据保存在 FE 上的 cookie 中,上述代码是否应该发送 cookie 或者 FE 可以将响应保存为 cookie 无关紧要?

UPDATE - If I was to use a cookie - Because cookie has an expiry, I think I can just use this rather than attempting to maintain expiry in db.更新- 如果我要使用 cookie - 因为 cookie 有一个过期时间,我想我可以使用它而不是试图在 db 中保持过期时间。 Will the below work?下面的工作吗?

userRoute.post('/login', (req, res) => {
if req.body.email === db.email
if req.body.password === db.password(hashed)    
updated and save db.userSecret
 send cookie (with userSecret, userToken)  with 30mins expiry


ProductRoute.post('/data', userAuth, (req, res) => {

Refresh cookie here ?
}


userAuth middleware:
If cookie received //i.e. not expired
    else re-direct to /login
    
Parse userSecret, userToken from COOKIE
fetch db collection where req.body.userToken === db.userToken
if userToken !== db.userToken && userSecret !==db.userSecret then error (401) - redirect to login
if db.expiryDate < 2mins remaining then renew db.expiryDate (save in db)
next()

I think you can add jwt-token in the headers in order to authenticate logged-in users.我认为您可以在标头中添加 jwt-token 以验证登录用户。

  1. Once the user logins in create a payload and create a jwt-token and add it to the response.用户登录后,创建有效负载并创建 jwt-token 并将其添加到响应中。

  2. On the subsequent request from the FE you can add the auth header with same token.在来自 FE 的后续请求中,您可以添加具有相同令牌的 auth header。

  3. On the backend, verify the token, if it is expired try to refresh it and follow the step first again.在后端,验证令牌,如果它已过期,请尝试刷新它并再次执行第一步。

There are several tutorials available online to help you create jwt-token.有几个在线教程可帮助您创建 jwt-token。 You can check here .你可以在这里查看 Also, this article is really good to setup authentication on node.js.此外,这篇文章非常适合在 node.js 上设置身份验证

Q1. Q1。 Before I implement I'm wondering if I've missed anything obvious in the steps.在我实施之前,我想知道我是否遗漏了步骤中的任何明显内容。

There are quite a few mistakes in it.其中有不少错误。 You should not save token on DB's instead use JWT-token to check the expiry, you can also use sessions in node.js您不应该将令牌保存在 DB 上,而是使用 JWT-token 检查到期时间,您也可以使用 node.js 中的会话

Q2. Q2。 Currently the front-end stores the userSecret and User token in local storage, and the server sends out a 200 response with userToken and userSecret (not a cookie) (see /login).目前,前端将 userSecret 和 User 令牌存储在本地存储中,服务器发送带有 userToken 和 userSecret(不是 cookie)的 200 响应(参见 /login)。 If I want to hold this data in a cookie on the FE, should the above code be sending a cookie instead or does it not matter as the FE can save the response asa cookie?如果我想将这些数据保存在 FE 上的 cookie 中,上述代码是否应该发送 cookie 或者 FE 可以将响应保存为 cookie 无关紧要?

To allow FE to save this response in the cookie, you need to create one at the backend.要让 FE 将这个响应保存在 cookie 中,您需要在后端创建一个。 When you attach it to the response browser on the subsequent request will attach it automatically.当您将其附加到响应浏览器时,后续请求将自动附加它。 Go through how cookies work . Go 通过cookies 如何工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM