Quellcode durchsuchen

:sparkles: passing token

master
TOJ vor 5 Jahren
Ursprung
Commit
0b42d68b3c

+ 2
- 0
backend/lib/plugins/user.js Datei anzeigen

@@ -10,6 +10,7 @@ const UserCurrentRoute = require('../routes/user/current');
10 10
 const UserLoginRoute = require('../routes/user/login');
11 11
 
12 12
 const UserService = require('../services/user');
13
+const DisplayService = require('../services/display');
13 14
 
14 15
 module.exports = {
15 16
     name: 'user-plugin',
@@ -32,6 +33,7 @@ module.exports = {
32 33
 
33 34
         await server.register(Schmervice)
34 35
         server.registerService(UserService)
36
+        server.registerService(DisplayService)
35 37
 
36 38
         await server.route(UserCurrentRoute)
37 39
         await server.route(UserLoginRoute)

+ 5
- 7
backend/lib/routes/user/login.js Datei anzeigen

@@ -18,10 +18,7 @@ const pluginConfig = {
18 18
 const validators = {
19 19
     post: {
20 20
         payload: Joi.object({
21
-            user: Joi.object().keys({
22
-                email: User.field('email'),
23
-                password: Joi.string()
24
-            }),
21
+            user: Joi.object(),
25 22
             error: Joi.string()
26 23
         })
27 24
     }
@@ -32,17 +29,18 @@ module.exports = {
32 29
     path: '/login',
33 30
     handler: async function (request, h) {
34 31
         try {
35
-            // const { userService, displayService } = request.services();
36
-            const { userService } = request.services()
32
+            const { userService, displayService } = request.services()
33
+
37 34
             const res = request.payload
38
-            console.log('---')
39 35
 
36
+            // Callback to use as transaction
40 37
             const login = async (txn) => {
41 38
                 return await userService.login({
42 39
                     email: res.user.email,
43 40
                     password: res.user.password
44 41
                 }, txn)
45 42
             }
43
+
46 44
             // Bound context from your plugin server declaration
47 45
             const user = await h.context.transaction(login)
48 46
             const token = userService.createToken(user.id)

+ 30
- 0
backend/lib/services/display.js Datei anzeigen

@@ -0,0 +1,30 @@
1
+'use strict';
2
+
3
+const Schmervice = require('@hapipal/schmervice');
4
+const internals = {};
5
+
6
+module.exports = class DisplayService extends Schmervice.Service {
7
+    user({ password, ...user }, token) {
8
+        return { ...user, token }
9
+    }
10
+
11
+    async profile(currentUserId, user, transaction)  {
12
+        const { User } = this.server.models()
13
+        const { toProfile } = internals
14
+
15
+        const result = await User.fetchGraph(user, `[
16
+            followedBy(currentUser) as following
17
+        ]`, {
18
+            transaction
19
+        }).modifiers({
20
+            currentUser: (builder) => builder.where('Users.id', currentUserId)
21
+        })
22
+
23
+        return toProfile(result)
24
+    }
25
+}
26
+
27
+internals.toProfile = ({ password, email, following, ...user }) => ({
28
+    ...user,
29
+    following: (following.length > 0)
30
+})

+ 14
- 11
backend/lib/services/user.js Datei anzeigen

@@ -47,24 +47,27 @@ module.exports = class UserService extends Schmervice.Service {
47 47
 
48 48
         console.log('user service attempting login...')
49 49
 
50
-        const user = await User.query(txn).throwIfNotFound().first().where({
51
-            email: User.raw('? collate nocase', email)
52
-        })
50
+        const user = await User.query(txn)
51
+            .throwIfNotFound()
52
+            .first()
53
+            .where({ user_email: email })
53 54
 
54
-        const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
55 55
 
56
-        if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
57
-            await this.changePassword(user.id, password, txn)
58
-        }
59
-        else if (passwordCheck !== SecurePassword.VALID) {
60
-            throw User.createNotFoundError()
61
-        }
56
+        // Uncomment to run password check using SecurePassword
57
+        // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
58
+        // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
59
+        //     await this.changePassword(user.id, password, txn)
60
+        // }
61
+        // else if (passwordCheck !== SecurePassword.VALID) {
62
+        //     throw User.createNotFoundError()
63
+        // }
62 64
 
63 65
         return user
64 66
     }
65 67
     createToken(id) {
68
+        const key =this.server.registrations['main-app-plugin'].options.jwtKey
66 69
         return Jwt.token.generate({ id }, {
67
-            key: this.options.jwtKey,
70
+            key: key,
68 71
             algorithm: 'HS256'
69 72
         }, {
70 73
             ttlSec: 7 * 24 * 60 * 60 // 7 days

Laden…
Abbrechen
Speichern