我有一个示例用户注册/登录Node.js / Angular应用程序在Angular 4上完全正常工作,但在我更新到5.1.0之后它给了我错误。
Express.js函数来验证用户身份:
""
Angular身份验证服务功能:
router.post('/authenticate', (req, res, next) => {
const username = req.body.username;
const password = req.body.password;
User.getUserByUsername(username, (err, user) => {
if(err) throw err;
if(!user) {
return res.json({ success: false, msg:'User not found' });
}
User.comparePassword(password, user.password, (err, isMatch) => {
if(err) throw err;
if(isMatch) {
const token = jwt.sign({ data: user }, config.secret, {
expiresIn: 604800
});
res.json({
success: true,
token: 'JWT ' + token,
user: {
id: user._id,
name: user.name,
username: user.username,
email: user.email
}
});
} else {
return res.json({ success: false, msg:'Wrong password' });
}
});
});
});
登录组件功能:
authenticateUser(user) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.httpClient.post('http://localhost:3002/users/authenticate',
user, { headers: headers });
}
错误输出:
onLoginSubmit() {
const user = {
username: this.username,
password: this.password
}
this.authService.authenticateUser(user).subscribe(data => {
if(data.success) {
this.authService.storeUserData(data.token, data.user);
this.flashMessagesService.show('You are now logged in',
{ cssClass: 'alert-success', timeout: 5000 });
this.router.navigate(['/dashboard']);
} else {
this.flashMessagesService.show(data.msg,
{ cssClass: 'alert-danger', timeout: 5000 });
this.router.navigate(['/login']);
}
});
}