Przeglądaj źródła

:pencil2: Fixed merge issues

tags/0.0.4
tomit4 2 lat temu
rodzic
commit
e60c0555cd
1 zmienionych plików z 69 dodań i 0 usunięć
  1. 69
    0
      backend/lib/routes/user/get-session.js

+ 69
- 0
backend/lib/routes/user/get-session.js Wyświetl plik

@@ -0,0 +1,69 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+
5
+const pluginConfig = {
6
+    handlerType: 'authentication',
7
+    docs: {
8
+        get: {
9
+            description: 'creates session token for authentication',
10
+            notes: 'Creates session token for authentication',
11
+        },
12
+    },
13
+}
14
+
15
+const validators = {
16
+    payload: Joi.object({
17
+        payload: Joi.object({
18
+            email: Joi.string(),
19
+            name: Joi.string(),
20
+            seeking: Joi.string(),
21
+        }),
22
+    }),
23
+}
24
+
25
+module.exports = {
26
+    method: 'POST',
27
+    path: '/token',
28
+    options: {
29
+        ...pluginConfig.docs.get,
30
+        tags: ['api'],
31
+        auth: false,
32
+        cors: {
33
+            headers: ['Authorization', 'Content-Type'],
34
+            exposedHeaders: ['Authorization', 'Access-Control-Expose-Headers'],
35
+        },
36
+        handler: async function (request, h) {
37
+            const { userService } = request.server.services()
38
+            const res = request.payload
39
+            // NOTE: Session Token set for 5 minutes expiration (default)
40
+            const sessionToken = await userService.createToken(res, 600)
41
+            try {
42
+                const response = h.response({
43
+                    ok: true,
44
+                    handler: pluginConfig.handlerType,
45
+                    data: sessionToken,
46
+                })
47
+                response.header('Authorization', sessionToken)
48
+                return response
49
+            } catch (err) {
50
+                return {
51
+                    ok: false,
52
+                    handler: pluginConfig.handlerType,
53
+                    data: {
54
+                        error: err,
55
+                    },
56
+                }
57
+            }
58
+        },
59
+        validate: {
60
+            ...validators,
61
+            failAction: 'log',
62
+        },
63
+        response: {
64
+            // TODO: change back to accommodate new h.response return values
65
+            schema: Joi.any().label('get_session_res'),
66
+            failAction: 'log',
67
+        },
68
+    },
69
+}

Ładowanie…
Anuluj
Zapisz