Saya mencoba membuat aplikasi CRUD untuk portofolio saya. Saya ingin pengguna dapat mendaftar dan masuk dan kemudian ke beberapa hal sekali di dalam (ini adalah pelacak kalori). Pengguna dapat Mendaftar tetapi ketika saya mencoba masuk, saya mendapatkan:

this.verify bukan fungsi

Berikut kode loginnya:

// Authenticate Login
app.post("/login", (req, res, next) => {
    passport.authenticate("local", {
        successRedirect: "/myprofile",
        failureRedirect: "/login",
        failureFlash: true
    })(req, res, next);
});

Paspor.js

const LocalStrategy = require("passport-local").Strategy,
      bcrypt = require("bcryptjs");

// User Model
const User = require("../models/user");

module.exports = function(passport) {
    passport.use(
        new LocalStrategy({usernameField:"username"}, {passReqToCallback: true}, (username, password, done) => {
            // Check if there is a user with this username
            User.findOne({
                username: username
            }).then(user => {
                    if(!user) {
                        return done(null, false, {message: "There is not a user with this username"});
                    }
                    // Check if the password is correct
                    bcrypt.compare(password, user.password, (err, isMatch) => {
                        if(err) throw err

                        if(isMatch) {
                            return done(null, user)
                        } else {
                            return done(null, false, {message: "Password is invalid"})
                        }
                    });
                })
                .catch(err => console.log(err));
        })
    );

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

    passport.deserializeUser((id, done) => {
        User.findById(id, (err, user) => {
            done(err, user)
        })
    })
}

Jika seseorang tertarik atau Anda memerlukan kode lagi, tentu saja saya dapat mempostingnya, saya tidak yakin harus memasukkan apa lagi. Tetapi jika Anda menyukainya, berikut adalah tautan ke Repo GitHub.

Terima kasih atas semua bantuan Anda, Tetap sehat dan sehat

Sunting1: Tumpukan penuh pertanyaan (atau saya harap ini dia)

TypeError: this._verify is not a function
    at Strategy.authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport-local\lib\strategy.js:90:12)
    at attempt (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:366:16)
    at authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:367:7)
    at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\app.js:187:7
    at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:275:10)
    at methodOverride (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\method-override\index.js:79:5)
    at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
0
Cortoloman 11 Juli 2020, 19:46

1 menjawab

Jawaban Terbaik

Jika Anda menggunakan opsi passReqToCallback paspor mengharapkan req-objek diteruskan sebagai argumen pertama dalam panggilan balik:

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'passwd',
    passReqToCallback: true,
  },
  function(req, username, password, done) {
    // request object is now first argument
    // ...
  }
));

Lihat ini untuk informasi selengkapnya.

1
eol 11 Juli 2020, 20:13