瀏覽代碼

pw migration model plugin route schema service

tags/0.0.1^2
diaseu 3 年之前
父節點
當前提交
1f2336a2f1

+ 11
- 0
backend/db/migrations/20220901171733_user_authentication.js 查看文件

1
+exports.up = function (knex) {
2
+    return knex.schema.createTable('authentication', function (table) {
3
+        table.string('user_email', 90).primary()
4
+        table.string('created_at').notNullable()
5
+        table.string('token').notNullable()
6
+    })
7
+}
8
+
9
+exports.down = function (knex) {
10
+    return knex.schema.dropTable('authentication')
11
+}

+ 1
- 1
backend/lib/models/authentication.js 查看文件

1
 const Schwifty = require('@hapipal/schwifty')
1
 const Schwifty = require('@hapipal/schwifty')
2
 const Joi = require('joi')
2
 const Joi = require('joi')
3
 
3
 
4
-module.exports = class userAuth extends Schwifty.Model {
4
+module.exports = class Auth extends Schwifty.Model {
5
     static get tableName() {
5
     static get tableName() {
6
         return 'authentication'
6
         return 'authentication'
7
     }
7
     }

+ 4
- 0
backend/lib/plugins/user.js 查看文件

5
 const JwtStrategy = require('../auth/strategies/jwt')
5
 const JwtStrategy = require('../auth/strategies/jwt')
6
 
6
 
7
 const UserModel = require('../models/user')
7
 const UserModel = require('../models/user')
8
+const AuthModel = require('../models/authentication')
8
 
9
 
9
 const UserCurrentRoute = require('../routes/user/current')
10
 const UserCurrentRoute = require('../routes/user/current')
10
 const UserProfileCreateRoute = require('../routes/user/create-profile')
11
 const UserProfileCreateRoute = require('../routes/user/create-profile')
11
 const UserProfilesListRoute = require('../routes/user/list-profiles')
12
 const UserProfilesListRoute = require('../routes/user/list-profiles')
12
 const UserLoginRoute = require('../routes/user/login')
13
 const UserLoginRoute = require('../routes/user/login')
13
 const UserSignupRoute = require('../routes/user/signup')
14
 const UserSignupRoute = require('../routes/user/signup')
15
+const UserPassword = require('../routes/user/authentication')
14
 
16
 
15
 const UserService = require('../services/user')
17
 const UserService = require('../services/user')
16
 const DisplayService = require('../services/display')
18
 const DisplayService = require('../services/display')
22
         await server.register(Jwt)
24
         await server.register(Jwt)
23
         await server.register(Schwifty)
25
         await server.register(Schwifty)
24
         await server.registerModel(UserModel)
26
         await server.registerModel(UserModel)
27
+        await server.registerModel(AuthModel)
25
 
28
 
26
         const mainApp = server.registrations['main-app-plugin']
29
         const mainApp = server.registrations['main-app-plugin']
27
         const jwtOptions = JwtStrategy(mainApp.options)
30
         const jwtOptions = JwtStrategy(mainApp.options)
44
         await server.route(UserSignupRoute)
47
         await server.route(UserSignupRoute)
45
         await server.route(UserProfileCreateRoute)
48
         await server.route(UserProfileCreateRoute)
46
         await server.route(UserProfilesListRoute)
49
         await server.route(UserProfilesListRoute)
50
+        await server.route(UserPassword)
47
     },
51
     },
48
 }
52
 }

+ 65
- 0
backend/lib/routes/user/authentication.js 查看文件

1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+const params = require('../../schemas/params')
5
+
6
+const pluginConfig = {
7
+    handlerType: 'password',
8
+    docs: {
9
+        get: {
10
+            description: 'get password',
11
+            notes: 'Returns a password by the user email passed in the path',
12
+        },
13
+    },
14
+}
15
+
16
+/** Validator functions by request method */
17
+const validators = {
18
+    /** Validate the route params (/active/{thing}) */
19
+    params: params.userEmail
20
+}
21
+
22
+module.exports = {
23
+    method: 'GET',
24
+    path: '/{user_email}/password',
25
+    options: {
26
+        ...pluginConfig.docs.get,
27
+        tags: ['api'],
28
+        // auth: 'default_jwt',
29
+        auth: false,
30
+        handler: async function (request, h) {
31
+            try {
32
+
33
+                const userEmail = request.params.user_email
34
+
35
+                const { userService } = request.services()
36
+                const password = userService.getPassword(userEmail)
37
+
38
+                return {
39
+                    ok: true,
40
+                    handler: pluginConfig.handlerType,
41
+                    data: { password: password },
42
+                }
43
+            } catch (err) {
44
+                return {
45
+                    ok: false,
46
+                    handler: pluginConfig.handlerType,
47
+                    data: { error: err },
48
+                }
49
+            }
50
+        },
51
+        validate: {
52
+            ...validators,
53
+            failAction: 'log',
54
+        },
55
+
56
+        response: {
57
+            schema: Joi.object({
58
+                ok: Joi.bool(),
59
+                handler: Joi.string(),
60
+                data: Joi.object(),
61
+            }).label('password_res'),
62
+            failAction: 'log',
63
+        },
64
+    },
65
+}

+ 2
- 1
backend/lib/schemas/params.js 查看文件

7
     userName: Joi.object({
7
     userName: Joi.object({
8
         name: Joi.string().min(3).max(11),
8
         name: Joi.string().min(3).max(11),
9
         all: Joi.array(),
9
         all: Joi.array(),
10
-    }).label('user_name_param')
10
+    }).label('user_name_param'),
11
+    userEmail: Joi.object({ user_email: Joi.string() }).label('user_email_param')
11
 }
12
 }

+ 11
- 0
backend/lib/services/user.js 查看文件

97
         return id
97
         return id
98
     }
98
     }
99
 
99
 
100
+
101
+
100
     /**
102
     /**
101
      * Self explanatory
103
      * Self explanatory
102
      * @param {*} param0
104
      * @param {*} param0
166
         //     })
168
         //     })
167
         // return id
169
         // return id
168
     }
170
     }
171
+
172
+    async getPassword(email, txn) {
173
+        const { Auth } = this.server.models()
174
+
175
+        const password = await Auth.query(txn)
176
+            .where('user_email', email)
177
+        
178
+        return password.token
179
+    }
169
 }
180
 }

Loading…
取消
儲存