简体   繁体   English

无法运行护照登录策略。 身份验证错误~类型错误:无法读取未定义的属性“身份验证”

[英]Trouble running passport login Strategy. Authentication error~ TypeError: Cannot read property 'authenticate' of undefined

Things I've tried我尝试过的事情

  • Moving the authenticate directly into the server将身份验证直接移动到服务器中
  • Uninstalling and reinstalling the following( as well as their corresponding types ):卸载和重新安装以下(以及它们对应的类型):
    • passport护照
    • passport-jwt护照-jwt
    • passport-local本地护照

All of these still result in getting the following error message :所有这些仍然导致收到以下错误消息
"TypeError: Cannot read property 'authenticate' of undefined"~Crashing the app “类型错误:无法读取未定义的属性‘身份验证’”~应用程序崩溃

Code代码

Server File服务器文件

import * as express from "express";
import apiRouter from "./routes/index";
import * as passport from 'passport';
import * as PassportLocal from 'passport-local';


import './middleware/passport-stradegies';

const app = express();


app.use(passport.initialize());
app.use(express.static("public"));
app.use(express.json());
app.use(apiRouter);

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server listening on port: ${port}`));



Passport Login Code护照登录码

import * as passport from 'passport';
import * as PassportLocal from 'passport-local';
import blogs from "../db/blogs";
import { compareHash } from "../utils";

passport.serializeUser((user, done)=> done(null, user));
passport.deserializeUser((user, done)=> done(null, user));

passport.use(new PassportLocal.Strategy({
    usernameField: 'email'
},async (email, password, done) =>{
    try {
         const [userFound] = await blogs.find("email", email);
          if (userFound && compareHash(password, userFound.userpassword)) {
            done(null, userFound);
    }else{
        done(null, false);
    }
    } catch (error) {
        done(error);
    }
}))

Login / Token Creation Page登录/令牌创建页面

import * as express from "express";
// import * as jwt from 'jsonwebtoken';
import { authenticate } from 'passport';
import config from '../../config';


const router = express.Router();

router.post("/", authenticate('local'), async (req, res, next) => {
   try {
  //     const token = jwt.sign({
  //       userid: userFound.id, email: userFound.email, role: 1}, 
  //       config.jwt.secret, {expiresIn: '15d'}
  //       );
  //     return res.json(token);
    res.json('plz');
  } catch (err) {
    console.log(err);
    res.status(500).json({ message: "My code login broke oops" });
  }
});

export default router;

The only problem is passport changed how authenticate is imported唯一的问题是护照更改了身份验证的导入方式

In login/token Authentication Page在登录/令牌认证页面

Change the import:更改导入:

// import { authenticate } from 'passport'; to
import * as passport from 'passport';

// then anywhere you use authenticate just use passport.authenticate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现Typec未被捕获的问题:无法读取未定义的属性“ 0” - Trouble with an Uncaught TypeError: Cannot read property '0' of undefined 类型错误:无法读取 reactjs 登录页面中未定义错误的属性“令牌” - TypeError: Cannot read property 'token' of undefined error in reactjs login page ......未捕获的类型错误:无法读取未定义的属性“登录” - ......Uncaught TypeError: Cannot read property 'login' of undefined 遇到未捕获的类型错误:无法读取未定义的属性 - Running into Uncaught TypeError: Cannot read property of undefined 如何修复TypeError:无法读取未定义的passportjs的属性“authenticate” - how to fix TypeError: Cannot read property 'authenticate' of undefined passportjs Passport.js github策略回调总是返回“TypeError:无法设置未定义的属性'用户'” - Passport.js github strategy callback always returns “TypeError: Cannot set property 'user' of undefined” 护照身份验证错误:策略#authenticate 必须被子类覆盖 - passport authentication error: Strategy#authenticate must be overridden by subclass 错误TypeError:无法读取未定义的属性'title' - ERROR TypeError: Cannot read property 'title' of undefined 错误TypeError:无法读取未定义的属性 - ERROR TypeError: Cannot read property of undefined 错误:TypeError:无法读取未定义的属性“ c” - Error: TypeError: Cannot read property 'c' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM