Bladeren bron

reworking wip

tags/0.0.1^2
diaseu 3 jaren geleden
bovenliggende
commit
eb48565eb6

+ 4
- 3
backend/db/migrations/20220901171733_user_authentication.js Bestand weergeven

@@ -1,8 +1,9 @@
1 1
 exports.up = function (knex) {
2 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()
3
+        table.string('user_email', 90).primary().unique()
4
+        table.date('created_at').notNullable()
5
+        // table.char('token').notNullable()
6
+        table.binary('token').notNullable()
6 7
     })
7 8
 }
8 9
 

+ 1
- 1
backend/lib/models/authentication.js Bestand weergeven

@@ -9,7 +9,7 @@ module.exports = class Auth extends Schwifty.Model {
9 9
         return Joi.object({
10 10
             user_email: Joi.string().required(),
11 11
             created_at: Joi.date().required(),
12
-            token: Joi.string().required()
12
+            token: Joi.binary().required()
13 13
         })
14 14
     }
15 15
 }

+ 1
- 0
backend/lib/routes/user/signup.js Bestand weergeven

@@ -26,6 +26,7 @@ const responseSchemas = {
26 26
         is_poster: Joi.number(),
27 27
         is_admin: Joi.number(),
28 28
         is_verified: Joi.number(),
29
+        user_pass: Joi.string()
29 30
     }).label('created_user'),
30 31
     error: errorSchema.single,
31 32
 }

+ 1
- 1
backend/lib/schemas/authentication.js Bestand weergeven

@@ -5,7 +5,7 @@ const Joi = require('joi')
5 5
 const userAuth = Joi.object({
6 6
     user_email: Joi.string(),
7 7
     created_at: Joi.date(),
8
-    token: Joi.string()
8
+    token: Joi.binary()
9 9
 }).label('user_auth')
10 10
 
11 11
 module.exports = {

+ 1
- 0
backend/lib/schemas/user.js Bestand weergeven

@@ -17,6 +17,7 @@ const userSignup = Joi.object({
17 17
     is_poster: Joi.number(),
18 18
     is_admin: Joi.number(),
19 19
     is_verified: Joi.number(),
20
+    user_pass: Joi.string()
20 21
 }).label('user_signup')
21 22
 
22 23
 module.exports = {

+ 38
- 17
backend/lib/services/user.js Bestand weergeven

@@ -95,23 +95,35 @@ module.exports = class UserService extends Schmervice.Service {
95 95
         if (matchingEmails.length > 0) {
96 96
             throw `User ${userInfo.user_email} already exists: Cannot create a user without a unique email`
97 97
         }
98
+        // const todayTest = new Date.now()
99
+        console.log("password passed to .signup()", password)
100
+        const steak = process.env.PEPPER+password
101
+        console.log("steak", steak)
102
+        console.log("user_email", userInfo.user_email)
98 103
 
99
-        // Library: Secure-Password
100
-        const pepper = process.env.PEPPER
101
-
102
-        // add pepper to pw and convert to buffer to prep for hash bytes
103
-        const steak = Buffer.from(password + pepper, 'utf-8')
104
-
105
-        // send peppered pw to (argon algorithm) library for salted hash
106
-        const hashed = await hasher(this.pwd, steak)
107
-        console.log("hashed", hashed)
108
-
109
-        const newAuth = await Auth.query(txn).insert({
104
+        const { email } = await Auth.query(txn).insert({
110 105
             user_email: userInfo.user_email,
111 106
             created_at: new Date.now(),
112
-            token: hashed,
113 107
         })
114
-        console.log("newAuth", newAuth)
108
+        await this.changePassword(email, steak, txn)
109
+        return userInfo.user_email
110
+        console.log("signup return finished")
111
+        // Library: Secure-Password
112
+        // console.log('data type of create_at', )
113
+        // add pepper to pw and convert to buffer to prep for hash bytes
114
+        // const steak = Buffer.from(password + pepper, 'utf-8')
115
+        // console.log("steak", steak)
116
+        // send peppered pw to (argon algorithm) library for salted hash
117
+        // hashed is actually for logging in
118
+        // const hashed = await hasher(this.pwd, steak)
119
+        // console.log("hashed", hashed)
120
+        // console.log ("user_email", userInfo.user_email)
121
+        // const newAuth = await Auth.query(txn).insert({
122
+        //     user_email: userInfo.user_email,
123
+        //     created_at: new Date.now(),
124
+        //     token: steak,
125
+        // })
126
+        // console.log("newAuth", newAuth)
115 127
         // return newAuth
116 128
 
117 129
         // const user = await User.query(txn).insert(userInfo)
@@ -202,10 +214,19 @@ module.exports = class UserService extends Schmervice.Service {
202 214
      * @param {*} txn
203 215
      * @returns {number}
204 216
      */
205
-    async changePassword(id, password, txn) {
206
-        const { User } = this.server.models()
207
-        return 'done'
208
-        // rework with Auth model
217
+    async changePassword(email, password, txn) {
218
+        const { User, Auth } = this.server.models()
219
+
220
+        await Auth.query(txn)
221
+            .throwIfNotFound()
222
+            .where({ email })
223
+            .patch({
224
+                // user_email: email,
225
+                token: await this.pwd.hash(Buffer.from(password)),
226
+            })
227
+        console.log("changed pw return", email)
228
+        console.log("token created in changePassword", this.pwd.hash(Buffer.from(password)))
229
+        return email
209 230
 
210 231
         // await User.query(txn)
211 232
         //     .throwIfNotFound()

Laden…
Annuleren
Opslaan