next 不是函数。 Node.js http://localhost:3000/api/authenticate
next is not a function. Nodejs http://localhost:3000/api/authenticate
我正在使用 jwt 和 passport 开发一个普通的应用程序 login/registration。使用本地存储我正在保存令牌,但它没有保存在本地存储中。
它还说 next is not a function
在我的 controller.js
中。另一方面,注册工作正常。
contoller.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;
}
);
}
user.service
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
}
希望对您有所帮助。
我正在使用 jwt 和 passport 开发一个普通的应用程序 login/registration。使用本地存储我正在保存令牌,但它没有保存在本地存储中。
它还说 next is not a function
在我的 controller.js
中。另一方面,注册工作正常。
contoller.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;
}
);
}
user.service
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
}
希望对您有所帮助。