简体   繁体   English

对多个网站使用节点passport.js

[英]using node passport.js for multiple websites

I am writing a node.js server to act as a REST server proxy for another server. 我正在编写一个node.js服务器,以充当另一台服务器的REST服务器代理。 Basically, it is going to allow multiple websites to authenticate, and provide a REST api for them to use. 基本上,它将允许多个网站进行身份验证,并提供一个REST API供他们使用。

I am going to use passport.js. 我将使用passport.js。

Below is the relevant section from the tutorial that I have a question about: 以下是我有问题的教程中的相关部分:

app.post('/login',
        passport.authenticate('local', { successRedirect: '/',
                                        failureRedirect: '/login'
                                        })
);

Is there a way to redirect to the website's root directory? 有没有一种方法可以重定向到网站的根目录? The website doesn't have the same URL as the node server, so how do I tell it to redirect to the website's root directory? 该网站与节点服务器没有相同的URL,那么如何告诉它重定向到该网站的根目录? Or do I have to implement that logic in the website's UI Router (I am using angularJS for the framework in each of the websites), and to just check for a returned user object? 还是我必须在网站的UI路由器中实现该逻辑(我在每个网站的框架中都使用angularJS),并仅检查返回的用户对象?

Do I also handle session expiry in the website's UI Router as well? 我还可以在网站的UI路由器中处理会话到期吗?

If I do that, how can I invalidate the session from the server side? 如果这样做,如何从服务器端使会话无效?

Let's break this down: 让我们分解一下:

    how can I invalidate the session from the server side?

Use redis-sessions for this. 为此使用redis-sessions The whole control flow is to store the session id and a time stamp inside redis. 整个控制流程是将会话ID和时间戳存储在Redis中。 You can then flush redis at every n minutes to invalidate those sessions and then have your server check against redis to ensure the current session isn't expired. 然后,您可以每隔n分钟刷新一次Redis,以使这些会话无效,然后让服务器针对Redis进行检查,以确保当前会话未过期。 That's a high level description, here is a tutorial on saving PHP sessions in Redis , the same logic will map over for your node.js application. 这是一个高层次的描述,这是有关在Redis中保存PHP会话的教程,同样的逻辑将映射到您的node.js应用程序。

   Is there a way to redirect to the website's root directory? 

The following way is usually how I handle redirects: 通常,以下方式是我处理重定向的方式:

    response.writeHead(200, {
       'Location': 'targetwebsite.com/index.html'

    });
    response.end();

Please let me know if you have any questions! 请让我知道,如果你有任何问题!

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

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