简体   繁体   English

passport.authenticate 不是 function / User.findone 不是 function

[英]passport.authenticate is not a function / User.findone is not a function

**I can't figure out for the life of me of what I am doing wrong. **我一生都无法弄清楚我做错了什么。 I am very new to passport and js in general but any help would be appreciated, Everytime I try to run in, I get authenticate is not a function.一般来说,我对护照和 js 很陌生,但如果有任何帮助,我将不胜感激,每次我尝试运行时,我都会得到身份验证不是 function。 Then if I require passport in my htmlRoutes.js I get findOne is not a function.然后,如果我在 htmlRoutes.js 中需要护照,我会得到 findOne 不是函数。 Very confused **很困惑 **

This is my passport.js这是我的护照.js

const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const User = require("../routes/htmlRoutes");

passport.use(
new LocalStrategy({ usernameField: "email" }, function (
email,
password,
done
) {
User.findOne({ email: email }, function (err, user) {
  if (err) {
    return done(err);
  }
  if (!user) {
    return done(null, false, { message: "Incorrect Email..." });
  }
  if (!user.validPassword(password)) {
    return done(null, false, { message: "Incorrect password..." });
  }
  return done(null, user);
});
})
);

passport.serializeUser(function (user, done) {
done(null, user.id);
});

passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});

module.exports = passport;

My Schema我的架构

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const userSchema = new Schema({
first_name: {
type: String,
required: true,
},
last_name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
});

module.exports = mongoose.model("User", userSchema);

My htmlRoutes.js.我的 htmlRoutes.js。 In Here, if i change passport to require('passport'), I lose the error for authenticate but it doesnt make any sense to me to do it like that在这里,如果我将护照更改为 require('passport'),我会丢失身份验证错误,但这样做对我来说没有任何意义

const express = require("express");
const Router = express.Router();
const User = require("../models/User");
const passport = require("../config/passport");

Router.get("/", (req, res) => {
res.render("index");
});

Router.post(
"/login",
passport.authenticate("local", {
successRedirect: "/dashboard",
failureRedirect: "/login",
failureFlash: true,
})
);

Router.get("/dashboard", (req, res) => {
res.render("dashboard");
});

Router.post("/new/users", (req, res) => {
User.create({
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: req.body.password,
}).then((data) => {
res.send(data);
});
});

module.exports = Router;

welcome tom StackOverflow.欢迎汤姆 StackOverflow。 There are a couple of things that I can see.我可以看到几件事。

  1. You are not importing the usercollectionSchema into the passport config and rather the userRoutes.js which exports the Router, which most certainly doesn't consists the findOne() function.您没有将 usercollectionSchema 导入到passport配置中,而是将导出路由器的userRoutes.js导入,它肯定不包含findOne() function。
  2. Secondly, you can go ahead and use require('passport') in the route file.其次,您可以提前 go 并在路由文件中使用require('passport')

I'm just writing out the part which needs to be changed, everything else must be obvious.我只是写出需要更改的部分,其他一切都必须是显而易见的。

In your passport.js在您的 passport.js 中

...
const User = require("../models/userModel");
...

In your userRoutes.js file在您的 userRoutes.js 文件中

...
const passport = require("passport");
...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM