Bläddra i källkod

reworking wip

tags/0.0.1^2
diaseu 3 år sedan
förälder
incheckning
eb48565eb6

+ 4
- 3
backend/db/migrations/20220901171733_user_authentication.js Visa fil

1
 exports.up = function (knex) {
1
 exports.up = function (knex) {
2
     return knex.schema.createTable('authentication', function (table) {
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 Visa fil

9
         return Joi.object({
9
         return Joi.object({
10
             user_email: Joi.string().required(),
10
             user_email: Joi.string().required(),
11
             created_at: Joi.date().required(),
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 Visa fil

26
         is_poster: Joi.number(),
26
         is_poster: Joi.number(),
27
         is_admin: Joi.number(),
27
         is_admin: Joi.number(),
28
         is_verified: Joi.number(),
28
         is_verified: Joi.number(),
29
+        user_pass: Joi.string()
29
     }).label('created_user'),
30
     }).label('created_user'),
30
     error: errorSchema.single,
31
     error: errorSchema.single,
31
 }
32
 }

+ 1
- 1
backend/lib/schemas/authentication.js Visa fil

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

+ 1
- 0
backend/lib/schemas/user.js Visa fil

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

+ 38
- 17
backend/lib/services/user.js Visa fil

95
         if (matchingEmails.length > 0) {
95
         if (matchingEmails.length > 0) {
96
             throw `User ${userInfo.user_email} already exists: Cannot create a user without a unique email`
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
             user_email: userInfo.user_email,
105
             user_email: userInfo.user_email,
111
             created_at: new Date.now(),
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
         // return newAuth
127
         // return newAuth
116
 
128
 
117
         // const user = await User.query(txn).insert(userInfo)
129
         // const user = await User.query(txn).insert(userInfo)
202
      * @param {*} txn
214
      * @param {*} txn
203
      * @returns {number}
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
         // await User.query(txn)
231
         // await User.query(txn)
211
         //     .throwIfNotFound()
232
         //     .throwIfNotFound()

Laddar…
Avbryt
Spara