login.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. module.exports = {
  2. friendlyName: 'Login',
  3. description: 'Log in using the provided email and password combination.',
  4. extendedDescription:
  5. `This action attempts to look up the user record in the database with the
  6. specified email address. Then, if such a user exists, it uses
  7. bcrypt to compare the hashed password from the database with the provided
  8. password attempt.`,
  9. inputs: {
  10. emailAddress: {
  11. description: 'The email to try in this attempt, e.g. "irl@example.com".',
  12. type: 'string',
  13. required: true
  14. },
  15. password: {
  16. description: 'The unencrypted password to try in this attempt, e.g. "passwordlol".',
  17. type: 'string',
  18. required: true
  19. },
  20. rememberMe: {
  21. description: 'Whether to extend the lifetime of the user\'s session.',
  22. extendedDescription:
  23. `Note that this is NOT SUPPORTED when using virtual requests (e.g. sending
  24. requests over WebSockets instead of HTTP).`,
  25. type: 'boolean'
  26. }
  27. },
  28. exits: {
  29. success: {
  30. description: 'The requesting user agent has been successfully logged in.',
  31. extendedDescription:
  32. `Under the covers, this stores the id of the logged-in user in the session
  33. as the \`userId\` key. The next time this user agent sends a request, assuming
  34. it includes a cookie (like a web browser), Sails will automatically make this
  35. user id available as req.session.userId in the corresponding action. (Also note
  36. that, thanks to the included "custom" hook, when a relevant request is received
  37. from a logged-in user, that user's entire record from the database will be fetched
  38. and exposed as \`req.me\`.)`
  39. },
  40. badCombo: {
  41. description: `The provided email and password combination does not
  42. match any user in the database.`,
  43. responseType: 'unauthorized'
  44. // ^This uses the custom `unauthorized` response located in `api/responses/unauthorized.js`.
  45. // To customize the generic "unauthorized" response across this entire app, change that file
  46. // (see http://sailsjs.com/anatomy/api/responses/unauthorized-js).
  47. //
  48. // To customize the response for _only this_ action, replace `responseType` with
  49. // something else. For example, you might set `statusCode: 498` and change the
  50. // implementation below accordingly (see http://sailsjs.com/docs/concepts/controllers).
  51. }
  52. },
  53. fn: async function (inputs, exits) {
  54. // Look up by the email address.
  55. // (note that we lowercase it to ensure the lookup is always case-insensitive,
  56. // regardless of which database we're using)
  57. var userRecord = await User.findOne({
  58. emailAddress: inputs.emailAddress.toLowerCase(),
  59. });
  60. // If there was no matching user, respond thru the "badCombo" exit.
  61. if(!userRecord) {
  62. throw 'badCombo';
  63. }
  64. // If the password doesn't match, then also exit thru "badCombo".
  65. await sails.helpers.passwords.checkPassword(inputs.password, userRecord.password)
  66. .intercept('incorrect', 'badCombo');
  67. // If "Remember Me" was enabled, then keep the session alive for
  68. // a longer amount of time. (This causes an updated "Set Cookie"
  69. // response header to be sent as the result of this request -- thus
  70. // we must be dealing with a traditional HTTP request in order for
  71. // this to work.)
  72. if (inputs.rememberMe) {
  73. if (this.req.isSocket) {
  74. sails.log.warn(
  75. 'Received `rememberMe: true` from a virtual request, but it was ignored\n'+
  76. 'because a browser\'s session cookie cannot be reset over sockets.\n'+
  77. 'Please use a traditional HTTP request instead.'
  78. );
  79. } else {
  80. this.req.session.cookie.maxAge = sails.config.custom.rememberMeCookieMaxAge;
  81. }
  82. }//fi
  83. // Modify the active session instance.
  84. this.req.session.userId = userRecord.id;
  85. // Send success response (this is where the session actually gets persisted)
  86. return exits.success();
  87. }
  88. };