-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
45 lines (36 loc) · 1.16 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// import passport from 'passport';
// import {Strategy} from 'passport-jwt'
// import {ExtractJwt} from 'passport-jwt'
var passport = require('passport');
var passportJwt = require('passport-jwt');
module.exports = function (app) {
var Usuarios = app.db.models.Usuarios;
var cfg = app.libs.config;
var Strategy = passportJwt.Strategy;
var opts = {};
opts.secretOrKey = cfg.jwtSecret;
opts.jwtFromRequest = passportJwt.ExtractJwt.fromAuthHeader();
passport.use('usuario',new Strategy(opts, function(playload, done){
var promise = Usuarios.findById(playload._id);
promise.then(function (usuario) {
// if(usuario && !usuario.bloqueado){
if(usuario){
return done(null, usuario);
}else{
app.libs.logger.error('Erro usuario invalido: ' + playload._id);
}
return done(null, false);
}).catch(function (err) {
app.libs.logger.error(err);
return done(null, false);
});
}));
return {
initialize: function() {
return passport.initialize();
},
authenticate: function(permissao){
return passport.authenticate(permissao, cfg.jwtSession);
}
};
};