Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to mix passport-facebook and passport-jwt?

I am kind of new to Node.js development and currently working on a pet project on my free time.

So far I have created JWT authentication using passport and passport-jwt for the strategy and I am using it in all of my RESTful APIs.

Now I am thinking of mixing this with some sort of Facebook authentication still want to stick with token authentication.

Currently this is how I am generating and obtaining the token:

exports.authenticate = function(req, res) {
    User.findOne({
        email: req.body.email
    }, function(err, user) {
        if (err)
            return res.status(400).send(getErrorMessage(err));

        if (!user) {
            res.status(400).send({
                success: false,
                message: 'Authentication failed. User not found.'
            });
        } else {
            if (user.checkPassword(req.body.password)) {

                let token = jwt.encode(user, config.secretPhrase);

                res.json({
                    success: true,
                    token: 'JWT ' + token
                });
            } else {
                res.status(401).send({
                    success: false,
                    message: 'Authentication failed. Wrong password.'
                });
            }
        }
    });
};

app.route('/api/users/authenticate')
        .post(user.authenticate);

And to validate I do the following:

let user = require('../../app/controllers/user-controller');
app.route('/api/todos')
        .get(user.validateLogin, todos.list)
        .post(user.validateLogin, todos.create);

user-controller:

exports.validateLogin = passport.authenticate('jwt', {
    session: false
});

Anyone can suggest a neat way to mix the two strategies ? should I use express-jwt ? What's the difference between express-jwt and passport-jwt ?

like image 452
Tohid Avatar asked Apr 21 '16 14:04

Tohid


People also ask

Which is better Passport or JWT?

JSON Web Token and Passport can be primarily classified as "User Management and Authentication" tools. JSON Web Token and Passport are both open source tools. It seems that Passport with 15.9K GitHub stars and 936 forks on GitHub has more adoption than JSON Web Token with 2.59K GitHub stars and 259 GitHub forks.

How do I use my Passport JWT strategy?

fromHeader('authorization'), secretOrKey: config. secret }; //Create JWT Strategy passport. use(new JwtStrategy(jwtOptions, function(payload, done){ //See if the user ID in the payload exists in our database //If it does, call 'done' with that user //otherwise, call done without a user object User. findById(payload.

What is the difference between Passport local and Passport JWT?

passport-local is the strategy you would use if you are authenticating against a username and password stored 'locally' i.e. in the database of your app - 'local' means local to your application server, not local to the end user. passport-jwt is the strategy for using JSON Web Tokens.


1 Answers

you can use passport-facebook like what you did with passport-jwt with new strategy so you can save Facebook user token in your database and return your token

like image 60
Maged Milad Avatar answered Nov 16 '22 00:11

Maged Milad