Kaynağa Gözat

:sparkles: tricky global context addition to use orm transactions

master
TOJ 5 yıl önce
ebeveyn
işleme
21d3168824

+ 10
- 8
backend/lib/plugins/user.js Dosyayı Görüntüle

@@ -1,12 +1,16 @@
1
+const Objection = require('objection');
1 2
 const Schmervice = require('@hapipal/schmervice');
2 3
 const Schwifty = require('@hapipal/schwifty');
3 4
 const Jwt = require('@hapi/jwt');
4 5
 const JwtStrategy = require('../auth/strategies/jwt')
6
+
5 7
 const UserModel = require('../models/user');
6 8
 
7 9
 const UserCurrentRoute = require('../routes/user/current');
8 10
 const UserLoginRoute = require('../routes/user/login');
9 11
 
12
+const UserService = require('../services/user');
13
+
10 14
 module.exports = {
11 15
     name: 'user-plugin',
12 16
     version: '1.0.0',
@@ -20,16 +24,14 @@ module.exports = {
20 24
         server.auth.strategy('default_jwt', 'jwt', jwtOptions)
21 25
         server.auth.default('default_jwt')
22 26
 
27
+        // Bind to global context
28
+        // So we can use Objection transactions
29
+        server.bind({
30
+            transaction: (fn) => Objection.transaction(server.knex(), fn)
31
+        })
23 32
 
24 33
         await server.register(Schmervice)
25
-        server.registerService(
26
-            class MathService extends Schmervice.Service {
27
-                add(x, y) {
28
-                    this.server.log(['math-service'], 'Adding')
29
-                    return Number(x) + Number(y)
30
-                }
31
-            }
32
-        )
34
+        server.registerService(UserService)
33 35
 
34 36
         await server.route(UserCurrentRoute)
35 37
         await server.route(UserLoginRoute)

+ 21
- 20
backend/lib/routes/user/login.js Dosyayı Görüntüle

@@ -18,10 +18,11 @@ const pluginConfig = {
18 18
 const validators = {
19 19
     post: {
20 20
         payload: Joi.object({
21
-            user: Joi.object().required().keys({
22
-                email: User.field('email').required(),
23
-                password: Joi.string().required()
24
-            })
21
+            user: Joi.object().keys({
22
+                email: User.field('email'),
23
+                password: Joi.string()
24
+            }),
25
+            error: Joi.string()
25 26
         })
26 27
     }
27 28
 }
@@ -29,35 +30,35 @@ const validators = {
29 30
 module.exports = {
30 31
     method: 'post',
31 32
     path: '/login',
32
-    handler: async (request, h) => {
33
+    handler: async function (request, h) {
33 34
         try {
34
-            const { user: { email, password } } = request.payload
35
-            const { mathService } = request.services()
36
-            console.log(mathService.add(10, 1))
37 35
             // const { userService, displayService } = request.services();
36
+            const { userService } = request.services()
37
+            const res = request.payload
38
+            console.log('---')
38 39
 
39
-            // const login = async (txn) => {
40
-
41
-            //     return await userService.login({ email, password }, txn);
42
-            // };
43
-
44
-            // const user = await h.context.transaction(login);
45
-            // const token = userService.createToken(user.id);
40
+            const login = async (txn) => {
41
+                return await userService.login({
42
+                    email: res.user.email,
43
+                    password: res.user.password
44
+                }, txn)
45
+            }
46
+            // Bound context from your plugin server declaration
47
+            const user = await h.context.transaction(login)
48
+            const token = userService.createToken(user.id)
46 49
 
47 50
             return {
48 51
                 ok: true,
49 52
                 handler: pluginConfig.handlerType,
50
-                data: {
51
-                    user: { email: '', password: '' }
52
-                },
53
-                // data: { user: displayService.user(user, token) }
53
+                data: { user: displayService.user(user, token) }
54 54
             }
55 55
         }
56 56
         catch(err) {
57
+            console.error(err)
57 58
             return {
58 59
                 ok: false,
59 60
                 handler: pluginConfig.handlerType,
60
-                data: { error: err },
61
+                data: { error: `${err}` },
61 62
             }
62 63
         }
63 64
     },

+ 2
- 0
backend/lib/services/user.js Dosyayı Görüntüle

@@ -45,6 +45,8 @@ module.exports = class UserService extends Schmervice.Service {
45 45
     async login({ email, password }, txn) {
46 46
         const { User } = this.server.models()
47 47
 
48
+        console.log('user service attempting login...')
49
+
48 50
         const user = await User.query(txn).throwIfNotFound().first().where({
49 51
             email: User.raw('? collate nocase', email)
50 52
         })

Loading…
İptal
Kaydet