我正在开发一个使用jwt和passport的平均应用程序登录/注册。使用本地存储我正在保存令牌,但它不会保存在本地存储中。
它还说我的next is not a function
中controller.js
。另一方面,注册工作非常顺利。
康托勒.js
module.exports.authenticate = (req, res, next) => {
// call for passport authentication
passport.authenticate('local', (err, user, info) => {
// error from passport middleware
if (err) return res.status(400).json(err);
// registered user
else if (user) return res.status(200).json({ "token":
user.generateJwt() });
// unknown user or wrong password
else return res.status(404).json(info);
})(req, res);
}
index.router.js索引
const express = require('express');
const router = express.Router();
const ctrlUser = require('../controllers/user.controller');
const jwtHelper = require('../config/jwtHelper');
router.post('/register', ctrlUser.register);
router.post('/authenticate', ctrlUser.authenticate);
router.get('/profile',jwtHelper.verifyJwtToken, ctrlUser.userProfile);
module.exports = router;
login.component.ts登录
onSubmit(form : NgForm){
this.userService.login(form.value).subscribe(
res => {
this.userService.setToken(res['token']);
this.router.navigateByUrl('/profile');
},
err => {
this.serverErrorMessages = err.error.message;
}
);
}
用户服务
login(authCredentials){
return this.http.post(environment.apiBaseUrl+'/authenticate', authCredentials, this.noAuthHeader);
}
setToken(token: string) {
localStorage.setItem('token', token);
}
getToken() {
return localStorage.getItem('token');
}
deleteToken() {
localStorage.removeItem('token');
}
getUserPayload() {
var token = this.getToken();
if (token) {
var userPayload = atob(token.split('.')[1]);
return JSON.parse(userPayload);
}
else
return null;
}
isLoggedIn() {
var userPayload = this.getUserPayload();
if (userPayload)
return userPayload.exp > Date.now() / 1000;
else
return false;
}
getUserProfile() {
return this.http.get(environment.apiBaseUrl + '/profile');
}
TypeError: next is not a function
at attempt (D:\fulinkedFolder\flogin\backend\node_modules\passport\lib\middleware\authenticate.js:186:32)
at authenticate (D:\fulinkedFolder\flogin\backend\node_modules\passport\lib\middleware\authenticate.js:362:7)
at module.exports.authenticate (D:\fulinkedFolder\flogin\backend\controllers\user.controller.js:37:7)
at Layer.handle [as handle_request] (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\layer.js:95:5)
at next (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\layer.js:95:5)
at D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:335:12)
at next (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:174:3)
at router (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:317:13)
at D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\fulinkedFolder\flogin\backend\node_modules\express\lib\router\index.js:335:12)
最佳答案
将next
函数与req
和res
一起传递:
module.exports.authenticate = (req, res, next) => {
passport.authenticate('local', (err, user, info) => {
// ...
})(req, res, next) // Pass 'next' here
}
我希望这能有帮助。
关于node.js - 接下来不是功能。 Node.js http://localhost:3000/api/authenticate,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56404243/