2

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

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

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.

2 Answers 2

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.

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):

<script>
    var myAccessToken = {amazon_access_token};

    // Angular code

</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Alright. But if I'm not using server side templates at all? I have AngularJS app on the frontend.
Use an ajax call from the frontend. If Node/Express isn't generating the HTML then the Angular app needs to request the token from the Node+Express app using the $http service. Or use sockets, but this would be overcomplicating things IMHO.
But how does my Angular app identify itself to request the token? How does the server differentiate between say 10 different Angular clients in this case?
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.

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

1 Comment

And don't forget to sign your cookies e.g. response.cookie("token", "1234567890", { signed: true, httpOnly: true });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.