Nodejs passport with local token with Mysql

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

I use passport-local token

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

But it looks like this will work with Mondomb.

Any idea?

+4
source share
3 answers

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

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

+1
source

, 100% Passport JS, - JSON, , jsonwebtoken:

, -, , , . , , , . , , . JSON, , Auth0, .

0

First of all, you can use any database you want with a passport or any other module, for example, let's say I use the mysql database ( MySQL npm package ), I just need 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);
                }
            });


    })
)
0
source

Source: https://habr.com/ru/post/1670727/


All Articles