Passport.js 上的登录不会在 Angular 上更新
Posted
技术标签:
【中文标题】Passport.js 上的登录不会在 Angular 上更新【英文标题】:Login on Passport.js does not update on Angular 【发布时间】:2017-05-01 23:03:32 【问题描述】:我正在构建我的第一个全栈 MEAN 应用程序,但我遵循的是旧教程,但使用了更新的 npm 包,因此我的一些代码不起作用。我目前正在构建一个登录/注册应用程序。我的程序在注册用户方面做得很好,如果用户尚未注册,它会存储它,如果它已经注册,则拒绝它。但是当我登录时,它应该将角度视图更改为登录并添加一个注销按钮,但它似乎没有这样做。
我在我的 html 上使用了 hashbang,我需要在我的代码中更改其他内容吗?
这是 Angular 应用的源代码
app.controller('authController', function($scope, $http, $rootScope, $location)
$scope.user = username: '', password: '';
$scope.error_message = '';
$scope.login = function()
$http.post('/auth/login', $scope.user).then(function(data)
if(data.state == 'success')
$rootScope.authenticated = true;
console.log('user is now authenticated'); // not printing
$rootScope.current_user = data.user.username;
console.log('user is now logged in');
$location.path('/');
console.log('view is updated');
else
$scope.error_message = data.message;
);
;
$scope.register = function()
$http.post('/auth/signup', $scope.user).then(function(data)
if(data.state == 'success')
$rootScope.authenticated = true;
console.log('user is now authenticated'); //this is not printing
$rootScope.current_user = data.user.username; //accessing mongodb
console.log('user is now logged in');
$location.path('/');
console.log('view is updated');
else
$scope.error_message = data.message;
);
;
);
这是认证路由的来源
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
module.exports = function(passport)
//sends successful login state back to angular
router.get('/success', function(req, res)
res.send(state: 'success', user: req.body.user ? req.body.user : null);
console.log('successfully login');
);
//sends failure login state back to angular
router.get('/failure', function(req, res)
res.send(state: 'failure', user: null, message: "Invalid username or password");
console.log('username/password does not exist in database');
);
//log in
router.post('/login', passport.authenticate('login',
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
));
//sign up
router.post('/signup', passport.authenticate('signup',
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
));
//log out
router.get('/signout', function(req, res)
req.logout();
res.redirect('/');
);
return router;
更新:
这可能与 PassportJS 有关吗?
var mongoose = require('mongoose');
var User = mongoose.model('User');
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');
module.exports = function(passport)
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done)
console.log('serializing user:',user.username);
done(null, user._id);
);
passport.deserializeUser(function(id, done)
User.findById(id, function(err, user)
console.log('deserializing user:',user.username);
done(err, user);
);
);
passport.use('login', new LocalStrategy(
passReqToCallback : true
,
function(req, username, password, done)
// check in mongo if a user with username exists or not
User.findOne( 'username' : username ,
function(err, user)
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user)
console.log('User Not Found with username '+username);
return done(null, false);
// User exists but wrong password, log the error
if (!isValidPassword(user, password))
console.log('Invalid Password');
return done(null, false); // redirect back to login page
// User and password both match, return user from done method
// which will be treated like success
return done(null, user);
);
));
passport.use('signup', new LocalStrategy(
passReqToCallback : true // allows us to pass back the entire request to the callback
,
function(req, username, password, done)
// find a user in mongo with provided username
User.findOne( 'username' : username , function(err, user)
// In case of any error, return using the done method
if (err)
console.log('Error in SignUp: '+err);
return done(err);
// already exists
if (user)
console.log('User already exists with username: '+username);
return done(null, false);
else
// if there is no user, create the user
var newUser = new User();
// set the user's local credentials
newUser.username = username;
newUser.password = createHash(password);
// save the user
newUser.save(function(err)
if (err)
console.log('Error in Saving user: '+err);
throw err;
console.log(newUser.username + ' Registration succesful');
return done(null, newUser);
);
);
)
);
var isValidPassword = function(user, password)
return bCrypt.compareSync(password, user.password);
;
// Generates hash using bCrypt
var createHash = function(password)
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
;
;
任何帮助或一般指针将不胜感激。谢谢!
【问题讨论】:
这似乎与角度无关。客户端有什么问题? 我正在使用 hashbang,也许就是这样?我更新了上面的一些源代码,当我调用 $location.path('/'); 时它似乎没有重定向我; 我正在使用 hashbang 也许是这样? @Phix 不认为我在 $scope.register 和 $scope.login 下的东西被执行了。 【参考方案1】:嗯,不要认为这与 passportJS 有任何关系,因为我在那里做的唯一事情就是散列和检查密码。
【讨论】:
以上是关于Passport.js 上的登录不会在 Angular 上更新的主要内容,如果未能解决你的问题,请参考以下文章
Passport.js Google 策略不适用于 React 应用程序
Sails.js + Passport.js 获取当前登录用户