2016-09-30 6 views
0

私はこの共通のアイデアへの回答を確認しましたが、かなりケースバイケースと思われ、私のコードではwthが間違っているとわかりません。ここで私は最初に自分のローカルサインアップを設定してからログインします。ログインしようとすると、入力に関係なく、 "Unknown strategy 'local'"というエラーが表示されます。未知の戦略 "local"、nodejs、パスポート

var express = require("express"); 
    var app = express(); 
    var mysql = require("mysql"); 
    var bodyParser = require("body-parser"); 
    var cookieParser = require("cookie-parser"); 
    var session = require('express-session'); 
    var passport = require('passport'); 
    var LocalStrategy = require('passport-local').Strategy; 

    // expose this function to our app using module.exports 
    module.exports = function(passport) { 
     passport.serializeUser(function(user, done) { 
     done(null, user.id); 
     }); 

     // used to deserialize the user 
     passport.deserializeUser(function(id, done) { 
     connection.query("select * from users where id = "+id,function(err,rows){ 
      done(err, rows[0]); 
     }); 
     }); 

     passport.use('local-signup', new LocalStrategy({ 
      // by default, local strategy uses username and password, we will override with email 
      usernameField : 'email', 
      passwordField : 'password', 
      passReqToCallback : true // allows us to pass back the entire request to the callback 
     }, 
     function(req, email, password, done) { 

      connection.query("select * from users where email = '"+email+"'",function(err,rows){ 
      console.log(rows); 
      console.log("above row object"); 
      if (err) 
        return done(err); 
      if (rows.length) { 
        return done(null, false, req.flash('signupMessage', 'That email is already taken.')); 
       } else { 

      // if there is no user with that email 
        // create the user 
        var newUserMysql = new Object(); 

      newUserMysql.email = email; 
        newUserMysql.password = password; // use the generateHash function in our user model 

      var insertQuery = "INSERT INTO users (email, password) values ('" + email +"','"+ password +"')"; 
       console.log(insertQuery); 
      connection.query(insertQuery,function(err,rows){ 
      newUserMysql.id = rows.insertId; 

      return done(null, newUserMysql); 
      }); 
       } 
     }); 
     })); 

     passport.use('local-login', new LocalStrategy({ 
      // by default, local strategy uses username and password, we will override with email 
      usernameField : 'email', 
      passwordField : 'password', 
      passReqToCallback : true // allows us to pass back the entire request to the callback 
     }, 
     function(req, email, password, done) { // callback with email and password from our form 

      connection.query("SELECT * FROM `users` WHERE `email` = '" + email + "'",function(err,rows){ 
      if (err) 
        return done(err); 
      if (!rows.length) { 
        return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash 
       } 

      // if the user is found but the password is wrong 
       if (!(rows[0].password == password)) 
        return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata 

       // all is well, return successful user 
       return done(null, rows[0]);  

     }); 



     })); 

    } 


    app.use(passport.initialize()); 


    ... 

編集:この部分を書き留めてください。イムでそれを呼び出す:

app.post('/login', 
    passport.authenticate('local', { successRedirect: '/', 
            failureRedirect: '/login', 
            failureFlash: true }) 
); 
+0

パスポートで戦略を設定しましたか? – abdulbarik

答えて

0

あなたはpassport.use('local-login', ...)としての戦略を定義しているので、私は(図示されていない)、あなたのエンドポイント定義でpassport.authenticate('local-login')を使用する必要がありますね。

+0

私はここにそれを書いているのを忘れましたが、うん、私はpassport.authenticateを使っています。 – petru

+0

'passport.authenticate( 'local'、...)'を 'passport.authenticate( 'local-login'、...)'に変更すると動作しますか? –

関連する問題