Я разрабатываю среднее приложение для входа / регистрации, используя jwt и паспорт.Используя локальное хранилище, я сохраняю токен, но он не сохраняется в локальном хранилище.Там же написано, что 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)