具有多个子域的 Express 和 Passport

Express & Passport with multiple subdomains

我正在将两个小应用程序合并为一个,它应该可以通过两个子域访问。为此,我使用了模块 "express-subdomain"。我正在尝试使用 Passport,因此当用户登录第一个应用程序时,他会同时登录两个应用程序。但是使用 req.isAuthenticated() 我确实可以登录 "first" 应用程序,但是我不在 "second" 应用程序中。

我正在寻找在第一个子域中进行护照身份验证并在第二个子域中登录的任何解决方案,包括在需要时保留两个不同的应用程序。

假设为本地策略正确配置了护照,以及路由器和应用程序的其余部分。这是它的样子:

app.js

// <- requires etc

var passport = require('./configurePassport.js')(require('passport'));

var app = express();

app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressSession({
    secret: 'bestSecretEver',
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
// and others

var firstRouter = express.Router();
firstRouter.use('/public', express.static(__dirname + '/first/public'));
firstRouter.use(favicon(__dirname + '/first/public/favicon.ico'));

// <- setting first router's GET, POST etc

app.use(subdomain('sub1', firstRouter)); // for sub1.example.com

var secondRouter = express.Router();
secondRouter.use('/public', express.static(__dirname + '/second/public'));
secondRouter.use(favicon(__dirname + '/second/public/favicon.ico'));

// <- setting second router's GET, POST etc

app.use(subdomain('sub2', secondRouter)); // for sub2.example.com

有什么想法吗?

查看您保存 cookie 的位置以进行身份​​验证。确保将其保存到根域。即:*.example.com 而不是 sub1.example.com