Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport JS session data in Angular2

What is the best way to send passport session informations from the back end to the frontend?

My application that works on port 3000. First two gets are for facebook login and redirection. Next one is to get user data from the database (user id should be stored in req.user)

routes.js:

app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

app.get('/auth/facebook/callback',
        passport.authenticate('facebook', {
            successRedirect : 'http://localhost:8000/',
            failureRedirect : '/fail'
        })
);

app.get('/auth/userdata', isLoggedIn, function(req, res) {
    Donator.findById(req.user, function(err, fulluser) {
        if (err) throw err;
        res.json(fulluser);
    })
});

function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) {
        next();
    } else {
        res.json(false);
    }
};

passport config.js

'facebookAuth' : {
        'clientID'      : 'secret',
        'clientSecret'  : 'secret',
        'callbackURL'   : 'http://localhost:3000/auth/facebook/callback'
    },

So in my Angular2 application I can go to the http://localhost:3000/auth/facebook, be redirected to the FB login page and if success redirected to the http://localhost:3000/auth/login/callback which takes me to the http://localhost:8000/.

And in my Angular2 application that works on port 8000

getUser(){
    this.http.get('http://localhost:3000/auth/userdata')
    .map(res => return res.json())
}

Everytime getUser() is called, it returns 'false'. Is there a simple and safe way to "inject" this session data to my frontend on the different port? Also when I go http://localhost:3000/auth/userdata in browser I can see this profile rendered as JSON.

When I set backend and frontend on the same port It works, facebook, twitter, google, local, everything is fine and getUser returns full user profile.

I hope it's clear.

like image 600
dmh126 Avatar asked Jan 16 '17 18:01

dmh126


People also ask

How to save passport user data in session?

On successful authentication, Passport saves the user data in the session. For that to happen, you need to invoke req.login with the user object. You also need to define the serialize and deserialize method to facilitate user data storage in the session and retrieving the data on subsequent requests.

What is passport in Node JS?

Passport.js is a middleware that can be easily used in your Node.js application. It helps to authenticate using username and password, Facebook, GitHub, etc.

How do I use passport and passport-local in JS?

You'll be using the passport-local strategy to authenticate the user login using a username and password. Require both passport and passport-local in app.js. const passport = require ( 'passport' ); const LocalStrategy = require ( 'passport-local' ).

Where does passport store the user data after authentication?

On successful authentication, Passport stores the user data in the session. On subsequent requests, Passport fetches the user data from the session for authentication. Let's make the Passport authentication call inside the custom middleware auth.


Video Answer


1 Answers

It was a problem with requests in the Angular2. I've added credentials to each request:

getUser(){
    this.http.get('http://localhost:3000/auth/userdata', {withCredentials: true})
    .map(res => return res.json())
}

And now it is fine.

like image 147
dmh126 Avatar answered Oct 19 '22 21:10

dmh126