将web标记从后端传递到前端(节点)。js +快递给AngularJS)

时间:2021-08-15 16:10:00

Being relatively new to JavaScript, I am trying to implement Amazon login in Node.js + Express / Angular.js app.

相对于JavaScript来说,我正在尝试在Node中实现Amazon登录。js + Express / angle。js应用。

Here is the piece from Node.js Passport authentication middleware example:

这是Node的文章。js Passport身份验证中间件示例:

app.get('/auth/facebook/callback', 
  passport.authenticate('service', { session: false }),
  function(req, res) {
    // Successful authentication, redirect home.
    // I have access to web token here
    res.redirect('/');
  });

The question is how do I modify the code above to pass web token that I obtain in function(req, res) in Node.js backend to AngularJS frontend assuming my AngularJS app is hosted at http://example.com while after logging in with Amazon, the app gets redirected to http://example.com/auth/facebook/callback URL.

问题是如何修改上面的代码来传递我在Node中的函数(req, res)中获得的web令牌。AngularJS前端的后端假定我的AngularJS应用托管在http://example.com上,而在与Amazon登录后,应用程序被重定向到http://example.com/auth/facebook/callback URL。

2 个解决方案

#1


3  

I'm going to assume you are using Express. As I understand it the access token will be stored somewhere in the req variable (eg. req.session.amazon....), having not used Amazon myself I'm not sure. This can then be passed to the Express renderer and included in a template.

我假设你在用快车。根据我的理解,访问令牌将存储在req变量中的某个位置(例如。req.session.amazon ....),不使用亚马逊自己我不确定。然后可以将其传递给Express渲染器,并将其包含在模板中。

res.render('myTemplate', {
    amazon_access_token : req.session.amazon.token // example
});

Then in your template (in this case handlebars in a file called myTemplate.hbs):

然后在模板中(在本例中,把手放在名为myTemplate.hbs的文件中):

<script>
    var myAccessToken = {amazon_access_token};

    // Angular code

</script>

#2


1  

Alright. The first way is to set it in a cookie like below. I still would like to know what the other options are.

好吧。第一种方法是将它设置为如下所示的cookie。我仍然想知道其他的选择是什么。

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        session: true
    }), function(req, res) {
        res.cookie('token', '1234567890', {
            maxAge: 3600000
        });
        res.redirect('/');
    }
);

#1


3  

I'm going to assume you are using Express. As I understand it the access token will be stored somewhere in the req variable (eg. req.session.amazon....), having not used Amazon myself I'm not sure. This can then be passed to the Express renderer and included in a template.

我假设你在用快车。根据我的理解,访问令牌将存储在req变量中的某个位置(例如。req.session.amazon ....),不使用亚马逊自己我不确定。然后可以将其传递给Express渲染器,并将其包含在模板中。

res.render('myTemplate', {
    amazon_access_token : req.session.amazon.token // example
});

Then in your template (in this case handlebars in a file called myTemplate.hbs):

然后在模板中(在本例中,把手放在名为myTemplate.hbs的文件中):

<script>
    var myAccessToken = {amazon_access_token};

    // Angular code

</script>

#2


1  

Alright. The first way is to set it in a cookie like below. I still would like to know what the other options are.

好吧。第一种方法是将它设置为如下所示的cookie。我仍然想知道其他的选择是什么。

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        session: true
    }), function(req, res) {
        res.cookie('token', '1234567890', {
            maxAge: 3600000
        });
        res.redirect('/');
    }
);