Tuesday 6 June 2017

Express + Passport error

I am using passport-facebook to create a SSO login for my app, the issue i am currently facing is I am unable to get any data from req.user when I make a call from the client.

I have seen other posts regarding this and followed their recommendation but still no luck

Here is my middleware

    this.app.use(logger('dev'));
    this.app.use(bodyParser.json());
    this.app.use(bodyParser.urlencoded({ extended: false }));
    this.app.use(session({ secret: 'keyboard cat' }));
    this.app.use(passport.initialize());
    this.app.use(passport.session());

The route i use to get send req.user

private validateUser(req, res, next):void{
    if (req.isAuthenticated()) { return next(); }
        res.redirect('/');
}

    router.get('/auth/userInfo', this.validateUser,  (req: any, res: any) => {
        res.json(req.user);
    });

My passport.js implementation

let passport = require('passport');
let FacebookStrategy = require('passport-facebook').Strategy;

class facebookAuth{

userId: string;
displayName: string;
email: string;
clientId: string;
secretId: string;

constructor() {
    this.clientId = facebookAppAuth.id;
    this.secretId = facebookAppAuth.secret;

    passport.use(new FacebookStrategy({
            clientID: this.clientId,
            clientSecret: this.secretId,
            callbackURL: "http://queuedapp.azurewebsites.net/auth/facebook/callback",
            profileFields: ['id', 'displayName', 'emails']
        },
        (accessToken, refreshToken, profile, done) => {
            process.nextTick( () => {
                console.log('validating facebook profile:' + JSON.stringify(profile));
                this.userId = profile.id;
                this.displayName = profile.displayName;
                this.email = profile.emails[0].value;
                return done(null, profile);
            });
        }
    ));

    passport.serializeUser((user, done) => {
        done(null, user);
    });

    passport.deserializeUser((user, done) => {
        done(null, user);
    });
}

}



via RRP

No comments:

Post a Comment