简体   繁体   English

Passport.js无状态

[英]Passport.js Stateless

I'm using Passport.js local strategies to handle auth in my app. 我正在使用Passport.js本地策略来处理我的应用程序中的身份验证。 I'm also using Nodemon to automatically refresh the server whenever I make changes. 每当进行更改时,我还使用Nodemon自动刷新服务器。

Problem is whenever I make changes I have to login again to the application. 问题是,每当我进行更改时,我都必须再次登录该应用程序。 For now this is just development but the same concept would apply to multiple servers handling the requests (ex. EC2 load balancer). 目前,这只是开发,但相同的概念将适用于处理请求的多台服务器(例如EC2负载平衡器)。 So my question is, how can I make Passport.js stateless while still preserving the state of the user? 所以我的问题是,如何在保持用户状态的同时使Passport.js无状态?

Seems like there has to be a way to preserve the state across servers and/or restarts of the server. 似乎必须要有一种方法来保留服务器之间的状态和/或重新启动服务器。

You have two options: 您有两种选择:

  1. Use a persistent session store eg MongoDB , Redis , or PostgreSQL 使用持久性会话存储,例如MongoDBRedisPostgreSQL
  2. Do not use sessions at all, use JSON Web Tokens aka JWT instead. 完全不要使用会话,而应使用JSON Web令牌(又名JWT)。

First option requires less setup if you already have a shared database, just instantiate the store and pass it to you app, eg: 如果您已经有一个共享数据库,则第一个选项需要较少的设置,只需实例化存储并将其传递给您的应用即可,例如:

const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

app.use(session({
    secret: 'foo',
    store: new MongoStore(options)
}));

The second option requires a different Passport strategy like passport-jwt or maybe ditching Passport completely and using jsonwebtoken directly in a custom middleware. 第二个选项需要不同的Passport策略,例如passport-jwt,或者完全放弃Passport并直接在自定义中间件中使用jsonwebtoken

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

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