Logging in users(用户登陆)

登陆

调用 User.login() 方法实现 登陆/认证, 需要提供一个带有 password 和 email/username 属性的对象,作为函数的第一个参数, 方法返回一个 access token

代码案例:

  1. /**
  2. * 登陆方法
  3. * @param {object} credentials username/password or email/password
  4. * @param {array/string} include 可选, 设置为'user' 回调的 token, 会带有 user 的信息, array ?
  5. * @param {function} callback(err, accessToken){ }
  6. */
  7. User.login({username: 'foo', password: 'bar'}, [include],function(err, accessToken) {
  8. console.log(accessToken);
  9. // {
  10. // ttl: 1209600,
  11. // userId: 574078e2fdecae6105b9be3e,
  12. // created: Sat May 21 2016 23:04:02 GMT+0800 (CST),
  13. // id: 'xS7Ge8T9WFEV17o0wK3KIqaU3M98QvlIcPKCTkaT0ZzWcdANGF7mKCkE89K18G1M'
  14. // }
  15. });
  16. myUserModel.login({username: 'foo', password: '123'}, 'user',function(err, accessToken) {
  17. debug(accessToken);
  18. // {
  19. // ttl: 1209600,
  20. // userId: 574087e1bc38978c0768f27a,
  21. // created: Sun May 22 2016 00:08:01 GMT+0800 (CST),
  22. // id: '1wU88tlNMoO3KlyypHSOXmcBiVlSF2QKlC4qOjFqSZN8xsRgpTZrzV4Sg2ZGHbxp',
  23. // user: {
  24. // username: 'foo',
  25. // password: '$2a$10$NNeLcFekDP5XGW4Wqa/LM.55twmAZq0daoNpsyzRa9j3iDRsXzqha',
  26. // email: 'foo@bar.com',
  27. // id: 574087e1bc38978c0768f27a
  28. // }
  29. // }
  30. cb();
  31. });

也可以指定一个ttl (time to live) 属性来指定 accessToken 的过期时间(seconds)

登出

使用 User.logout() 方法登出.

  1. /**
  2. * 用户登出
  3. * @param {string} 用户的accessToken
  4. */
  5. User.logout(req.accessToken.id, function(err) {
  6. if (err) return next(err);
  7. res.redirect('/'); //on successful logout, redirect
  8. });
  9. /**
  10. * 在服务器端(server side only)
  11. * 可以使用user的实例
  12. */
  13. User.findOne({email: 'foo@bar.com'}, function(err, user) {
  14. user.logout();
  15. });

Login using third-party systems(第三方登陆系统)

使用 第三方 登出插件,可以替代 用户的登陆系统.

更多详情: Third-party login using Passport

Resetting a user’s password(重置user密码)

使用 User.resetPassword() 方法重置用户密码,此方法创建临时登录一个短暂的访问令牌,使如果忘记了用户更改密码.(机制??)

流程:

  1. 调用User.resetPassword()方法
  2. 发送一封带有short-lived access token的邮件
  3. 使用 updateAttribute() 更新密码
  1. // 使用邮箱,创建一个 short-lived 的 accessToken
  2. app.post('/request-password-reset', function(req, res, next) {
  3. User.resetPassword({
  4. email: req.body.email
  5. }, function(err) {
  6. if (err) return res.status(401).send(err);
  7. res.render('response', {
  8. title: 'Password reset requested',
  9. content: 'Check your email for further instructions',
  10. redirectTo: '/',
  11. redirectToLinkText: 'Log in'
  12. });
  13. });
  14. });
  15. // 发送忘记密码的短信, 带有 accessToken
  16. user.on('resetPasswordRequest', function(info) {
  17. var url = 'http://' + config.host + ':' + config.port + '/reset-password';
  18. var html = 'Click <a href="' + url + '?access_token=' +
  19. info.accessToken.id + '">here</a> to reset your password';
  20. //'here' in above html is linked to : 'http://<host:port>/reset-password?access_token=<short-lived/temporary access token>'
  21. user.app.models.Email.send({
  22. to: info.email,
  23. from: info.email,
  24. subject: 'Password reset',
  25. html: html
  26. }, function(err) {
  27. if (err) return console.log('> error sending password reset email');
  28. console.log('> sending password reset email to:', info.email);
  29. });
  30. });
  31. // 更新 password
  32. User.findById(req.accessToken.userId, function(err, user) {
  33. if (err) return res.sendStatus(404);
  34. user.updateAttribute('password', req.body.password, function(err, user) {
  35. if (err) return res.sendStatus(404);
  36. console.log('> password reset processed successfully');
  37. res.render('response', {
  38. title: 'Password reset success',
  39. content: 'Your password has been reset successfully',
  40. redirectTo: '/',
  41. redirectToLinkText: 'Log in'
  42. });
  43. });
  44. });