TVImageElement
文件passport.js
express.js
'use strict';
let express = require('express');
let bodyParser = require('body-parser');
let cors = require('cors');
let cookieParser = require('cookie-parser');
let session = require('express-session');
let passport = require('passport');
let load = require('express-load');
module.exports = () => {
let app = express();
//configurar a porta em que o express irá rodar
app.set('port', 3000, "127.0.0.1");
//configura como os dados são transitados entres servidor e cliente
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(require('method-override')());
app.use('*', cors());
app.use(cookieParser());
app.use(session({
secret: 'ilovenodejs',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
load('models', {cwd: 'app'})
.then('controllers')
.then('routes')
.into(app);
return app;
}
路线护照:
'use strict';
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var mongoose = require('mongoose');
var configAuth = require('./auth');
module.exports = () => {
var Usuario = mongoose.model('Usuario');
passport.use(new FacebookStrategy({
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL
},
function (accessToken, refreshToken, profile, done) {
//console.log('profile: ', profile);
process.nextTick(() => {
Usuario.findOne({'facebook.id': profile.id}, (err, usuario) => {
if (err) return done(err);
if (usuario) {
//console.log('user: ', user);
return done(null, usuario);
} else {
var usuario = new Usuario();
usuario.facebook.id = profile.id;
usuario.facebook.token = accessToken;
usuario.facebook.name = profile.displayName;
usuario.facebook.email = 'email';
usuario.save((err) => {
if (err) throw err;
return done(null, usuario);
});
}
});
});
}
));
passport.serializeUser(function(usuario, done){
console.log('serialize', usuario._id);
done(null, usuario._id);
});
passport.deserializeUser(function(id, done){
console.log('deserialize', id);
Usuario.findById(id).exec()
.then(function(usuario){
console.log('deserialize', usuario);
done(null, usuario);
});
});
}
这部分护照路线正在重定向到我的vuejs客户端。
部分认证路线:
'use strict';
//let cors = require('cors');
let passport = require('passport');
module.exports = (app) => {
let controller = app.controllers.usuarios;
let Usuario = app.models.usuario;
let temp = '';
let objetoUsuario = {}
//app.use(cors());
app.route('/api/usuarios')
.get(controller.listaUsuarios)
.post(controller.salvaUsuario);
app.get('/auth/facebook', passport.authenticate('facebook'),
function (req, res) { });
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/' }),
function (req, res) {
temp = req.session.passport;
console.log(req.session.passport);
req.session.save(() => {
req.user = req.session.passport
res.redirect('http://localhost:8080/#!/home');
});
});
app.get('/v1/success', (req, res) => {
Usuario.findById(temp.user).exec()
.then((usuario) => {
if (!usuario) throw new error('Usuario não encontrado');
res.json(usuario);
req.session.passport.user = usuario
}, (err) => {
res.status(404).json(err);
});
//console.log(req.session.passport);
});
app.get('/logout', (req, res) => {
req.logout();
res.redirect('http://localhost:8080/#!/home');
});
}
记录passeport控制台。 passeport会议是空的,不知道为什么。
'use strict';
function verificaAutenticacao(req, res, next){
console.log(req.user);
console.log(req.session);
if(req.isAuthenticated()){
return next();
}else{
res.status('401').json('não autorizado');
}
};
module.exports = (app) => {
let controller = app.controllers.viagens;
app.route('/api/viagens')
.post(verificaAutenticacao, controller.salvaViagem);
}
我知道我做错了什么但不知道这是我的错误。 用户身份验证正常完成,如果我执行console.log(req.session.passport),会话中有用户的id,但是当我去验证session.passport时这个空了,现在我不知道是什么做。 如果有人可以帮助我。 谢谢。