2015-02-27 23 views
8

Tôi mới dùng Nodej và tìm kiếm quản lý chứng chỉ người dùng, tôi thấy Hộ chiếu là một tùy chọn tốt.Nodejs + Hộ chiếu: Cách thêm thông tin người dùng khác

Nhưng trong chiến lược đăng ký hộ chiếu, tôi chỉ thấy thông tin người dùng được lưu là email và mật khẩu.

tôi cần thông tin người dùng giống như họ tên, công việc, thời gian đăng ký, cuối cùng thời gian hoạt động, vv

Vậy làm thế nào tôi có thể làm điều này trong hộ chiếu?

Trả lời

5

Bên trong chiến lược đăng ký/đăng ký Passport.js của bạn, bạn có thể chuyển đối tượng yêu cầu như tham số đầu tiên của hàm đó và Hộ chiếu sẽ xử lý yêu cầu của bạn vào chức năng cho bạn.

Vì vậy, bạn sẽ có thể sử dụng đối tượng req.body, lấy dữ liệu từ biểu mẫu của bạn theo cách đó và lưu trữ dữ liệu đó vào cơ sở dữ liệu của bạn.

Dưới đây là ví dụ chi tiết hơn về cách thức hoạt động của tính năng này.

passport.use('signup', new LocalStrategy({ 
    passReqToCallback : true 
    }, 
    function(req, username, password, done) { 
    findOrCreateUser = function(){ 
     // find a user in Mongo with provided username 
     User.findOne({'username':username},function(err, user) { 
     // In case of any error return 
     if (err){ 
      console.log('Error in Signup: ' + err); 
      return done(err); 
     } 
     // already exists 
     if (user) { 
      console.log('User already exists'); 
      return done(null, false, 
      req.flash('message','User Already Exists')); 
     } else { 
      // if there is no user with that email 
      // create the user 
      var newUser = new User(); 
      // set the user's local credentials 
      newUser.username = username; 
      newUser.password = createHash(password); 
      newUser.firstName = req.body.firstName; 
      newUser.lastName = req.body.lastName; 

      // save the user 
      newUser.save(function(err) { 
      if (err){ 
       console.log('Error in Saving user: '+err); 
       throw err; 
      } 
      console.log('User Registration succesful');  
      return done(null, newUser); 
      }); 
     } 
     }); 
    }; 

    // Delay the execution of findOrCreateUser and execute 
    // the method in the next tick of the event loop 
    process.nextTick(findOrCreateUser); 
    }); 
); 

Dưới đây là một tutorial bao gồm chi tiết hơn một chút. Tôi đã thay đổi các tham số firstName và lastName từ các tham số thành các biến trong cơ thể. Nhưng bạn có thể sử dụng thông số hoặc cơ thể để đưa dữ liệu đó vào chiến lược địa phương của bạn.

-1

Tôi thấy rằng điều này hoạt động tốt hơn. Nó có vẻ là chìa khóa mà thực hiện được sau khi req, tên người dùng, mật khẩu, nhưng trước khi các biến khác mà bạn mong muốn để vượt qua vào chức năng. Nếu hoàn thành được đặt vào cuối thì:

events.js:160 throw er; // Unhandled 'error' event TypeError: done is not a function 

sẽ được trả lại.

// ========================================================================= 
// LOCAL SIGNUP ============================================================ 
// ========================================================================= 
// we are using named strategies since we have one for login and one for signup 
// by default, if there was no name, it would just be called 'local' 

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

    // asynchronous 
    // User.findOne wont fire unless data is sent back 
    process.nextTick(function() { 

    // 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 

      User.findOne({ 'local.username' : username }, function(err, user) { 
       // if there are any errors, return the error 
       if (err) 
        return done(err); 

       // check to see if theres already a user with that email 
       if (user) { 
        return done(null, false, req.flash('signupMessage', 'That username is already taken.')); 
       } else { 

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

        // set the user's local credentials 
        newUser.local.fname = fname; 
        newUser.local.lname = lname; 
        newUser.local.username = username; 
        newUser.local.email = email; 
        newUser.local.password = newUser.generateHash(password); 
        newUser.local.security_question = security_question; 
        newUser.local.security_answer = newUser.generateHash(security_answer); 

        // save the user 
        newUser.save(function(err) { 
         if (err) 
          throw err; 
         return done(null, newUser); 
        }); 
       } 
      }); 




    }); 
})); 
+0

args req, tên người dùng và mật khẩu được thực hiện đều được ... nhưng làm cách nào chúng tôi có thể chuyển các thông số bổ sung như fname, lname, v.v ...? trước hết, thực sự từ đâu nó sẽ lấy tên người dùng và mật khẩu ..? từ req.body hoặc từ một số nơi khác ..? Cảm ơn trước.. – shivaraj

Các vấn đề liên quan