ソースを参照

pw migration model plugin route schema service

tags/0.0.1^2
diaseu 3年前
コミット
1f2336a2f1

+ 11
- 0
backend/db/migrations/20220901171733_user_authentication.js ファイルの表示

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

+ 4
- 0
backend/lib/plugins/user.js ファイルの表示

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

+ 65
- 0
backend/lib/routes/user/authentication.js ファイルの表示

@@ -0,0 +1,65 @@
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,5 +7,6 @@ module.exports = {
7 7
     userName: Joi.object({
8 8
         name: Joi.string().min(3).max(11),
9 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,6 +97,8 @@ module.exports = class UserService extends Schmervice.Service {
97 97
         return id
98 98
     }
99 99
 
100
+
101
+
100 102
     /**
101 103
      * Self explanatory
102 104
      * @param {*} param0
@@ -166,4 +168,13 @@ module.exports = class UserService extends Schmervice.Service {
166 168
         //     })
167 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
 }

読み込み中…
キャンセル
保存