2016-08-17 6 views
-1

MySql、Express、Angular、NodeJSアプリケーションを開発しています。ユーザー登録について頭を落とそうとしています。情報を提供するプロッパーソースが見つかりません。探しています。Passportとmysqlを使用してユーザーを作成する

私はユーザー名とパスワードを使ってユーザーレコードを作成する方法を知っていますが、データベースをテキストとして保存し、ハッシュ化/ソルトしていない方法だけを知っています。

私は、ユーザーがサインアップするときにハッシュパスワードを作成する方法を知る必要があると思っています。次に、そのハッシュされたパスワードをデータベースに配置する方法を見つけ、最後にユーザーがサインインしたいときにパスワードを比較します。しかし、それはすべて少し抽象的で、私はパスポートの情報がこれのいずれかを見つけることができないと感じます。

+0

Plzこれをチェックしてくださいhttps://gist.github.com/manjeshpv/84446e6aa5b3689e8b84 –

答えて

0

我々はplzはこれをチェックする私たちは、デフォルトでのログイン用とサインアップ ための1つを持っているので何の名前がなかった場合には、それだけで「ローカル」と呼ばれます詳細については

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) { 

    // find a user whose email is the same as the forms email 
    // we are checking to see if the user trying to login already exists 
    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); 
      }); 
     } 
    }); 
})); 

という名前の戦略を使用していますhttps://gist.github.com/manjeshpv/84446e6aa5b3689e8b84

+0

私の問題点を理解しました。私は、パスポートがハッシュされたパスワードを取得する方法を見ているのに困っています。私は登録時にパスワードを取得し、ハッシュして保存する方法についてのいくつかの例を見てきました.dbがハッキングされたときに実際のパスワードがそこにはありません。しかし、ユーザーがログインすると、パスワードが正しいかどうかをパスポートはどのように知っているのですか?ハッシュされているからです。 –

関連する問題