Friday 14 April 2017

How to use passport library to build Facebook login?Whats wrong in my code?

I am trying to include a Facebook login using the passport library. My code seems to be just like in their documentation but when I click on the Facebook login button, the page starts flashing like crazy and nothing else happens. Im also not sure if my mistake is how I registered my app with Facebook. I had to provide a site url and only put http://www.localhost/ as I haven't deployed the app. Any help or tipp would be awesome! Thank you!!!

server/app/configure/auth/index.js:

    'use strict';
    var path = require('path');
    var session = require('express-session');
    var passport = require('passport');
    var SequelizeStore = require('connect-session-sequelize')(session.Store);

    module.exports = function(app, db) {

        var dbStore = new SequelizeStore({
            db: db
        });

        var User = db.model('user');

        dbStore.sync();

        app.use(session({
            secret: app.getValue('env').SESSION_SECRET,
            store: dbStore,
            resave: false,
            saveUninitialized: false
        }));

        app.use(passport.initialize());
        app.use(passport.session());

        passport.serializeUser(function(user, done) {
            done(null, user.id);
        });

        passport.deserializeUser(function(id, done) {
            User.findById(id)
                .then(function(user) {
                    done(null, user);
                })
                .catch(done);
        });

        app.get('/session', function(req, res) {
            if (req.user) {
                res.send({
                    user: req.user.sanitize()
                });
            } else {
                res.status(401).send('No authenticated user.');
            }
        });

        app.get('/logout', function(req, res) {
            req.logout();
            res.status(200).end();
        });

        ENABLED_AUTH_STRATEGIES.forEach(function(strategyName) {
            require(path.join(__dirname, strategyName))(app, db);
        });

    };

server/app/configure/auth/facebook.js:

    var path = require('path');
    var session = require('express-session');
    var passport = require('passport');
    var SequelizeStore = require('connect-session-sequelize')(session.Store);

    var ENABLED_AUTH_STRATEGIES = [
        'local',
        //'twitter',
        //'facebook',
        //'google'
    ];

    module.exports = function(app, db) {

        var dbStore = new SequelizeStore({
            db: db
        });

        var User = db.model('user');

        dbStore.sync();

        app.use(session({
            secret: app.getValue('env').SESSION_SECRET,
            store: dbStore,
            resave: false,
            saveUninitialized: false
        }));

        app.use(passport.initialize());
        app.use(passport.session());

        passport.serializeUser(function(user, done) {
            done(null, user.id);
        });

        passport.deserializeUser(function(id, done) {
            User.findById(id)
                .then(function(user) {
                    done(null, user);
                })
                .catch(done);
        });

        app.get('/session', function(req, res) {
            if (req.user) {
                res.send({
                    user: req.user.sanitize()
                });
            } else {
                res.status(401).send('No authenticated user.');
            }
        });


        app.get('/logout', function(req, res) {
            req.logout();
            res.status(200).end();
        });


        ENABLED_AUTH_STRATEGIES.forEach(function(strategyName) {
            require(path.join(__dirname, strategyName))(app, db);
        });

    };

server/env/development:

    module.exports= {
        "DATABASE_URI": "postgres://localhost:5432/xxx",
        "SESSION_SECRET": "secret apple",
        "FACEBOOK": {
            "clientID": "19611111111111", 
            "clientSecret": "f7500111111111111111",         
            "callbackURL": "http://localhost:1337/auth/facebook/callback"
        }
        ,
        "LOGGING": true
    };

server/

HTML:

<a href="/auth/facebook" >Sign in with Facebook</a>



via javascript2016

No comments:

Post a Comment