在子域上与 redis 和通行证共享会话? [英] Share sessions with redis and passport on a subdomain?
问题描述
我正在使用 npm 子域,在我的应用程序中,我有伪造子域的路由
I am using npm subdomain, inside my app I have routes that fake a subdomain
// looks like app.localhost:3000
router.get('/subdomain/app', app.dashboard);
所以我在子域上有一个登录页面,在非子域页面上有一个登录页面.他们不共享会话,所以我必须登录两次.我想设置redis,但我不知道如何设置.
So I have a login page on a subdomain and a login page on a non subdomain page. They don't share sessions, so I have to login twice. I want to setup redis, but I don't know how.
// here is my session middleware, I tried using .localhost
app.use(session({ secret: 'something', domain: '.localhost', }));
我见过人们在哪里使用像
I have seen where people are using redis like
app.use(express.session({
store:new RedisStore({
host: config.redis.session.host,
port: config.redis.session.port,
db: config.redis.session.db
}),
secret: config.session_secret
}));
这似乎可以解决我的问题,但我不知道如何设置 redisStore 以及配置数据的来源?
This seems like it could solve my issue but I have no clue how to setup a redisStore and where the config data comes from?
有人可以向我解释一下如何使用 redis 以便当用户登录 app.example.io 或 example.io 时,他/她已永久登录,无需登录两次?
Can someone explain to me how to use redis so that when a user logs in on either app.example.io or example.io that he/she is logged in for good, no need to log in twice?
推荐答案
这是我是怎么做的,这是最重要的部分 - 域:.yourdomain.io在您的域之前加点.
Here is how I did it, this is the most important piece - domain: .yourdomain.io Make sure to have that preceeding dot before your domain.
var express = require('express'),
app = express(),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
expressSession = require('express-session'),
sessionMiddleware = null,
redis = require('redis'),
conn_redis = {
path: '/var/run/redis/redis.sock',
socket_keepalive: true
}
app.use(cookieParser())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
const connectRedis = require('connect-redis')(expressSession),
uid = require('uid-safe').sync
sessionMiddleware = expressSession({
store: new connectRedis(conn_redis),
secret: 'xxxxxxxxxxxxxxxxxxxxx',
name: 'session_name',
resave: false,
rolling: true,
saveUninitialized: false,
logErrors: true,
cookie: {
path: '/',
domain: '.yourdomain.io'
expires: new Date(Date.now() + 3600000),
maxAge: 3600000
}
})
这篇关于在子域上与 redis 和通行证共享会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!