接下来不是功能。 Node.js http:// localhost:3000 / api / authenticate

时间:2019-06-01 06:00:19

标签: node.js api jwt passport.js restful-authentication

我正在使用jwt和passport开发平均的应用登录/注册。使用本地存储,我正在保存令牌,但未将其保存在本地存储中。 它还说我的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)

1 个答案:

答案 0 :(得分:0)

next函数与reqres一起传递:

module.exports.authenticate = (req, res, next) => {
  passport.authenticate('local', (err, user, info) => {

    // ...

  })(req, res, next) // Pass 'next' here
}

我希望这会有所帮助。