Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Signup Endpoint #27

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion backend/src/controllers/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
AuthenticationControllerInterface,
AuthenticationServiceInterface,
} from "../../types.js";
import { BadRequestError } from "../errors/errors.js";

class AuthenticationController implements AuthenticationControllerInterface {
constructor(private authentication: AuthenticationServiceInterface) {}
constructor(private authenticationService: AuthenticationServiceInterface) {}

postLogin = async (
req: Request,
Expand All @@ -21,6 +22,36 @@ 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")
}
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)
}

};

postLogout = async (
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middlewares/session.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import session from "express-session";
import redis from "redis";
import { SESSION_SECRET, REDIS_URL } from "../config/config";
import { SESSION_SECRET, REDIS_URL } from "../config/config.js";
const RedisStore = require("connect-redis")(session);

const redisClient = redis.createClient({
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserInterface } from "../../types.js";

class User implements UserInterface {
export class User implements UserInterface {
constructor(
private email: string,
private password: string,
Expand Down
8 changes: 5 additions & 3 deletions backend/src/services/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { UserRepositoryInterface } from "../../types";
import UserRepository from "../repositories/user.repository";
import {User} from "../models/user.model"

class AuthenticationService implements AuthenticationService {
constructor(private UserRepository: UserRepositoryInterface) {}
constructor(private userRepository: UserRepositoryInterface) {}

async login(): Promise<void> {
// TODO: Implement login and initialize session (Anfaal)
}

async signup(): Promise<void> {
async signup(email: string, password:string, firstName:string, lastName:string): Promise<void> {
// TODO: Implement signup and initialize session (Ryan)
const newUser = new User(email,password,firstName,lastName)
this.userRepository.createUser(newUser)
}

async logout(): Promise<void> {
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);
});
});
});
2 changes: 1 addition & 1 deletion backend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface AuthenticationControllerInterface {

export interface AuthenticationServiceInterface {
login(): Promise<void>;
signup(): Promise<void>;
signup(email: string, password:string, firstName:string, lastName:string): Promise<void>;
logout(): Promise<void>;
}

Expand Down
Loading