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