我已经完成并查看了很多Passport JS的例子。可悲的是,他们中没有一个像我一样。当我在server.js
文件中使用护照时,它就像我复制的代码一样。
但是我没有得到passport.authenticate()在src/server/login/routes.js
我也无法将其作为POST工作,它只能作为GET使用,我不知道为什么。
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var passport = require('passport');
var passportLocal = require('passport-local');
var User = require('server/db/db').User;
var routes = require('server/routes');
var PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(cookieParser());
app.use(expressSession({
secret: process.env.SESSION_SECRET || 'mySecret',
resave: true,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passportLocal.Strategy(function(username, password, done) {
console.log('localStrat Username: ' + username + ' Password: ' + password);
User.findOne({username: username, password: password}, function(err, user) {
console.log('searched for user');
if(user) {
console.log('found');
console.log(user);
done(null, user);
} else {
console.log('none');
done(null, false, {message: 'Unable to login'});
}
});
// if(username === password) {
// done(null, {id: username, name: username});
// } else {
// done(null, false, {message: 'Unable to login'});
// }
}));
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
done(null, {id: id, name: id});
});
routes(app);
app.get('/', function(req, res) {
res.send('\
<!DOCTYPE html>\
<html>\
<head>\
<title>TCG Inventory Manager</title>\
<base href="/">\
</head>\
<body>\
<div ui-view></div>\
<script src="bundle.js"></script>\
</body>\
</htm>\
');
});
app.get('/login', passport.authenticate('local'), function(req, res) {
console.log('/login');
res.send(req.user);
});
import angular from 'angular';
import uiRouter from 'angular-ui-router';
import uiBootstrap from 'angular-ui-bootstrap';
import todoFactory from 'factories/todo-factory';
import invManFactory from 'factories/invMan-factory';
import loginFactory from 'factories/login-factory';
import todosController from 'todos/todos';
import invController from 'invMan/invMan';
import loginController from 'login/login';
const app = angular.module('app', [uiRouter, todoFactory.name, invManFactory.name, loginFactory.name, uiBootstrap]);
app.config(($stateProvider, $urlRouterProvider, $locationProvider) => {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('todos', {
url: '/todo',
template: require('todos/todos.html'),
controller: todosController
})
.state('about', {
url: '/about',
template: require('about/about.html')
})
.state('invMan', {
url: '/invMan',
template: require('invMan/invMan.html'),
controller: invController,
resolve: { loginCheck: checkLogin }
})
.state('login', {
url: '/',
template: require('login/login.html'),
controller: loginController
})
$locationProvider.html5Mode(true);
});
var checkLogin = function($q, $timeout, $http, $location, $rootScope) {
console.log('checkLogin');
var deffered = $q.defer();
$http.get('/login/stuff/loggedIn').success(function(user) {
$rootScope.errorMessage = null;
// User is Authenticated
if(user !== '0') {
console.log('is authed');
$rootScope.currentUser = user;
deffered.resolve();
// User is not Authenticated
} else {
console.log('not authed');
$rootScope.errorMessage = 'You need to be log in.';
deffered.reject();
$location.url('/');
}
});
return deffered.promise;
};
export default app;
var loginRoutes = require('server/login/routes');
var todosRoutes = require('server/todos/routes');
var invManRoutes = require('server/invMan/routes');
module.exports = function routes(app, passport) {
app.use('/todos', todosRoutes);
app.use('/invMan', invManRoutes);
app.use('/login/stuff', loginRoutes);
};
var express = require('express');
var mongoose = require('mongoose');
var User = require('server/db/db').User;
var router = express.Router();
router.get('/', passport.authenticate('local'), function(req, res) {
console.log('login');
res.send(req.body);
});
router.get('/loggedIn', function(req, res) {
res.send(req.isAuthenticated() ? req.user : '0');
});
module.exports = router;
提前感谢您的帮助!
答案 0 :(得分:0)
您可以在需要检查身份验证时传递此功能。
router.get('/', ensureAuthentication, function(req, res) {
console.log('login');
res.send(req.body);
});
function ensureAuthentication(req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
//user is not logged in
}
}