Skip to content

Commit

Permalink
Password encryption and create user test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjung1998 committed Jan 13, 2024
1 parent 5b10c7c commit 54638c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
18 changes: 17 additions & 1 deletion backend/src/controllers/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,31 @@ class AuthenticationController implements AuthenticationControllerInterface {
next: NextFunction
): Promise<void | Response<any, Record<string, any>>> => {
// 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)
Expand Down
22 changes: 21 additions & 1 deletion backend/test/authentication.test.ts
Original file line number Diff line number Diff line change
@@ -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:"[email protected]",
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);
});
});
});

0 comments on commit 54638c3

Please sign in to comment.