Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

login.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict'
  2. const Joi = require('joi')
  3. const errorSchema = require('../../schemas/errors')
  4. const userSchema = require('../../schemas/user')
  5. const pluginConfig = {
  6. handlerType: 'user',
  7. docs: {
  8. description: 'login',
  9. notes: 'Attempt login',
  10. },
  11. }
  12. /** Validator functions by request method */
  13. const validators = {
  14. post: {
  15. payload: Joi.object({
  16. user: userSchema.single,
  17. error: errorSchema.single,
  18. })
  19. .append()
  20. .label('login_payload'),
  21. },
  22. user: userSchema.single,
  23. }
  24. module.exports = {
  25. method: 'POST',
  26. path: '/login',
  27. options: {
  28. ...pluginConfig.docs,
  29. tags: ['api'],
  30. auth: false,
  31. handler: async function (request, h) {
  32. try {
  33. const { userService, displayService } = request.services()
  34. const res = request.payload
  35. // Callback to use as transaction
  36. const login = async txn => {
  37. return await userService.login(
  38. {
  39. email: res.user.email,
  40. password: res.user.password,
  41. },
  42. txn,
  43. )
  44. }
  45. // Bound context from your plugin server declaration
  46. const user = await h.context.transaction(login)
  47. const token = userService.createToken(user)
  48. return {
  49. ok: true,
  50. handler: pluginConfig.handlerType,
  51. data: displayService.user(user, token),
  52. }
  53. } catch (err) {
  54. console.error(err)
  55. return {
  56. ok: false,
  57. handler: pluginConfig.handlerType,
  58. data: { error: `${err}` },
  59. }
  60. }
  61. },
  62. validate: validators.post,
  63. response: {
  64. schema: Joi.object({
  65. ok: Joi.bool(),
  66. handler: Joi.string(),
  67. data: validators.user,
  68. }).label('login_res'),
  69. failAction: 'log',
  70. },
  71. },
  72. }