在Express JS/中从域重定向到子域时,如何保持会话活动 [英] How to keep the session active when redirecting from domain to subdomain in Express js/

查看:56
本文介绍了在Express JS/中从域重定向到子域时,如何保持会话活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事Express项目(MEAN).我在myDomain.com上有我的登录/注册页面,登录后我想将用户重定向到user.myDomain.com.我正在使用会话存储模块将会话存储在MongoDb中.我可以将用户重定向到子域,但是由于会话不是在域和子域之间进行的..它将用户重定向到登录页面. 我已经查看并尝试了

I am working on an Express project(MEAN). i have my login/register page at myDomain.com and on login i want to redirect the user to user.myDomain.com . I am storing the session in MongoDb using session storage module. I can redirect the user to the subdomain but since the session is not maitained in between the domain and subdomain.. it redirects user to the login page. I have looked and tried everything from

使用Express和Node,如何在子域/主机头之间维护会话 https://github.com/jaredhanson/passport/issues/125 在nodejs mongostore中维护跨子域的登录会话

但是我仍然迷路,无法为此找到合适的解决方案. 你能帮我吗?

But i am still lost and could not find a proper solution for thise. Can you please help me.

修改: 我这样做了,但id并没有帮助我.

I did this but id did not help me.

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(cookieParser());
app.use(express.static('../app'));

app.use(session({
  secret:appConfig.SECRET,
  resave:false,
  saveUninitialized:true,
  domain: '.myDomain.com',
  store: sessionstore.createSessionStore({
      type: 'mongodb',
      host: 'localhost',         // optional
      port: 27017,               // optional
      dbName: 'MyDataBase',       // optional
      collectionName: 'sessions',// optional
      timeout: 10000             // optional
      // authSource: 'authedicationDatabase',        // optional
      // username: 'technicalDbUser',                // optional
      // password: 'secret'                          // optional
      // url: 'mongodb://user:pass@host:port/db?opts // optional
  }),
  cookie: {
         path: '/',
         domain: '.myDomain.com',
         maxAge: 1000 * 60 * 24 // 24 hours
     }
}));

推荐答案

我之前遇到过同样的问题. 也许您可以尝试这样:

I had the same problem before. Maybe you could have a try like this:

  app.use(session({
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 30,
    domain:'.dreamon.so' // That's what you need.
  },
  store: new RedisStore({
    host: settings.redis.HOST,
    port: settings.redis.PORT
  }),
  secret: settings.session.SECRET,
  resave: true,
  saveUninitialized: true
}));

但是请记住在进行其他测试之前清除浏览器cookie.

But remember to clear browser cookie before you make an other test.

这篇关于在Express JS/中从域重定向到子域时,如何保持会话活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆