简体   繁体   English

如何进行单用户认证和授权? 或者如何限制在 node js 中注册到应用程序的用户数量?

[英]How to make a single user authentication and authorization? Or how to restrict number of users registering to an application in node js?

I want to make a simple app using MERN ( Mongo , Express , React , Node ), in which only one person will have to upload pics or write a blog;我想使用MERN ( Mongo , Express , React , Node ) 制作一个简单的应用程序,其中只有一个人需要上传图片或写博客; clients can only view in general.客户只能查看一般情况。 So, I want to create one user authentication.所以,我想创建一个用户身份验证。 How can I achieve this?我怎样才能做到这一点?

  • Store sessions in the database将会话存储在数据库中
  • Add a field called, say, "inuse" to the session document在会话文档中添加一个名为“inuse”的字段
  • Set that field to 1 when storing the session存储会话时将该字段设置为 1
  • Add a unique index with that key使用该键添加唯一索引

This will prevent your application from being able to store more than one session at a time.这将阻止您的应用程序一次存储多个会话。

You would then need to devise a mechanism for expiring sessions that are abandoned (ie user doesn't explicitly click "log out" button) as well as figure out what to do if, say, your browser crashes and you can't use the established session but it's still within the session inactivity timeout.然后,您需要设计一种机制来使被放弃的会话过期(即用户没有明确单击“注销”按钮),并弄清楚如果您的浏览器崩溃并且您无法使用已建立会话,但它仍在会话不活动超时内。

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

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