简体   繁体   English

如何为单个用户(Node、Express)提供身份验证?

[英]How to provide authentication for a single user (Node, Express)?

I am currently working on an app with a React frontend and a Node/Express backend.我目前正在开发具有 React 前端和 Node/Express 后端的应用程序。 There are certain functions of the app that I only want an admin user to be able to use, so I want to implement an admin login form that takes a username and password.我只希望管理员用户能够使用该应用程序的某些功能,因此我想实现一个需要用户名和密码的管理员登录表单。 This would be the only user that this app has.这将是此应用程序拥有的唯一用户。

How would I go about implementing this?我将如何实施这个? I've looked at technologies like passport.js and express-basic-auth, but I'm wondering if there's a simpler way to implement this since there is only a single user.我看过像passport.js 和express-basic-auth 这样的技术,但我想知道是否有更简单的方法来实现它,因为只有一个用户。 Any advice would be greatly appreciated!任何建议将不胜感激!

If you wanna do it by yourself then whenever the user logs in the application, create an authtoken for the user and save it in the backend and send the authoken as the response for successful login.如果你想自己做,那么每当用户登录应用程序时,为用户创建一个 authtoken 并将其保存在后端并发送 authoken 作为成功登录的响应。 Further save the authtoken as a cookie on the frontend.进一步将 authtoken 保存为前端的 cookie。 Next will be while performing any action you need to send the authtoken to the server.接下来将执行您需要将 authtoken 发送到服务器的任何操作。 If that authtoken is present on the server side you go on with the action or else send the response error saying that the authtoken is invalid of something like that.如果服务器端存在该 authtoken,您将继续执行该操作,否则会发送响应错误,说明该 authtoken 无效。 On loggin out of the application remember to expire the cookie and also remove the authtoken from the backend.在退出应用程序时,请记住使 cookie 过期并从后端删除 authtoken。

You can add this if you want where if the authoken provided is incorrect you can simple remove the authtoken from the backend and redirect the user to login page expiring the cookie from the frontend.如果您想在提供的身份验证不正确的情况下添加它,您可以简单地从后端删除身份验证并将用户重定向到登录页面,从而使前端的 cookie 过期。

暂无
暂无

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

相关问题 带有节点和快速用户身份验证的Ember - Ember With Node and Express User Authentication 如何在单个快速路由 User.route('/').get((req,res,next)=>{ }) 上添加两个身份验证? - How to add two authentication on single express Route User.route('/').get((req,res,next)=>{ }) in this? 如何在Node Express应用中将Parse用户身份验证实现为中间件? - How should I implement Parse user authentication as middleware in Node Express app? 如何使用express-ntlm获得Windows用户名而无需使用NODE.js进行身份验证? - how to use express-ntlm to get windows user name without authentication using NODE.js? 如何使用节点和快递存储身份验证令牌 - How to store authentication token with node and express 为什么每个人都使用MongoDB通过Node.Js和Express进行用户身份验证 - Why everyone uses MongoDB for user authentication with Node.Js and Express Node / express应用程序跳过用户身份验证中间件,直接进入回调 - Node/express app skips user authentication middleware and goes straight to callback Node Express 认证/授权 - Node Express Authentication/authorisation 如何进行单用户认证和授权? 或者如何限制在 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? 使用express和sockjs进行用户身份验证 - user authentication with express and sockjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM