From 54638c344bdc6ff5ec243d968494a4404ad79462 Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Sat, 13 Jan 2024 10:19:19 -0700 Subject: [PATCH] Password encryption and create user test --- .../controllers/authentication.controller.ts | 18 ++++++++++++++- backend/test/authentication.test.ts | 22 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/backend/src/controllers/authentication.controller.ts b/backend/src/controllers/authentication.controller.ts index bcb03f6..1f1450e 100644 --- a/backend/src/controllers/authentication.controller.ts +++ b/backend/src/controllers/authentication.controller.ts @@ -22,15 +22,31 @@ class AuthenticationController implements AuthenticationControllerInterface { next: NextFunction ): Promise>> => { // TODO: Implement postSignup controller (Ryan) + const bcrypt = require('bcrypt'); + const saltRounds = 10; // Cost factor for hashing - 10 is a good balance between security and performance + var hashword = "failed"; try{ const email = req.body.email const password = req.body.password const firstName = req.body.firstName const lastName = req.body.lastName + //getting signup details from the body if(email == null || password == null || firstName == null || lastName == null){ throw new BadRequestError("All fields must be submitted") } - this.authenticationService.signup(email, password, firstName, lastName) + bcrypt.hash(password, saltRounds, (err: any, hash: string) => { + if (err) { + console.error('Error while hashing:', err); + + } else { + // console.log('Hashed Password:', hash); + hashword = hash; + } + }); + //encrypting password + const createdUser = this.authenticationService.signup(email, hashword, firstName, lastName);//creating the user? + // req.session.user = createdUser; //session doesn't have a user? + return } catch (err){ next(err) diff --git a/backend/test/authentication.test.ts b/backend/test/authentication.test.ts index 16356d3..596ebef 100644 --- a/backend/test/authentication.test.ts +++ b/backend/test/authentication.test.ts @@ -1,7 +1,27 @@ import request from "supertest"; import { assert } from "chai"; import { http } from "./config.test.js"; +var bcrypt = require('bcrypt'); +const testUser = { + email:"Ryan@mail.com", + password:"$2b$10$5jUD1fhvp1YLSE1FpnEa5eVpOZTOp5wivb6DtvrkeKfIO0/ZTCAqG", + firstName:"Ryan", + lastName:"Ryan" +} describe("Authentication", () => { - // TODO: Add tests for authentication and session management + it("Signup should return a 201 status code", async () => { + const res: request.Response = await http.post("/auth/signup").send({ + email:testUser.email, + password:testUser.password, + firstName:testUser.firstName, + lastName:testUser.lastName + }); + assert.equal(res.body.email,testUser.email); + assert.equal(res.body.firstName,testUser.firstName); + assert.equal(res.body.lastName,testUser.lastName); + bcrypt.compare("mySecurePassword123", res.body.password, function(err: any, result: boolean) { + assert(result); + }); + }); });