4

I want to user be able to start session with just token. No need to enter password.

I am using passport-local-token

https://www.npmjs.com/package/passport-local-token

But seems this will working with Mongodb.

Any idea?

Ankita Kashyap
  • 517
  • 6
  • 20
  • It's not very clear on what you want to ask. Can you rephrase your question? – manishg Feb 28 '17 at 22:44
  • `passport-local-token` works with any database. The *example* uses Mongo but you can use any database in the handler. It doesn't seem to be very widely used though, better to use eg. `passport-jwt` that was already mentioned. – vesse Mar 01 '17 at 16:48
  • http://stackoverflow.com/questions/17397052/nodejs-passport-authentication-token – bxN5 Mar 03 '17 at 17:10

3 Answers3

1

just install passport-jwt by using below command npm -i passport-jwt --save

https://jonathanmh.com/express-passport-json-web-token-jwt-authentication-beginners/

sachin raj
  • 90
  • 1
  • 5
0

I'm not sure by the post question if you're 100% sold on Passport JS, but if you're just looking for basic JSON web token functionality, checkout, jsonwebtoken:

They are pretty slick, and you can setup your web application so a token is generated by the server, passed back to your front-end, and then an authentication response is sent back to the server. Your token is setup on your server-side, and a secret passcode you create is used for encryption and decryption. I did a little project utilizing them and it's pretty cool. There are other JSON web tokens out there, but the one created by the Auth0 people is nice.

twknab
  • 1,741
  • 1
  • 21
  • 35
0

First of all you can use any kind of database you want with passport or any other module for example let's say i am using mysql database (MySQL npm package ) i will just have to adapt the code to sql standards

//Start Connection to DB
var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'my_db'
});

connection.connect();

passport.use('local-token', new LocalStrategy(
    function(token, done) {

        connection.query('SELECT accessToken from AccessToken where id: ?', [token],
            function (error, accessToken, fields) {
                if (error) return done(error);

                if (accessToken) {
                    if (!token.isValid(accessToken)) {
                        return done(null, false);
                    }

                    connection.query('SELECT * from User where id: ?', [accessToken.userId],
                        function (error, user, fields) {
                            if (error) return done(error);
                            if (!user) {
                                return done(null, false);
                            }

                            return done(null, user);
                        })
                } else {
                    return done(null);
                }
            });


    })
)
eGhoul
  • 2,490
  • 1
  • 13
  • 17