Skip to content

Commit

Permalink
Implemented user registration feature with input validation, password…
Browse files Browse the repository at this point in the history
… hashing, and database integration
  • Loading branch information
maxCastro1 committed Apr 30, 2024
1 parent 516fa4e commit ad7f09a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/express-winston": "^4.0.0",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
Expand All @@ -42,6 +43,7 @@
},
"devDependencies": {
"@eslint/js": "^9.1.1",
"@types/bcrypt": "^5.0.2",
"@types/body-parser": "^1.19.5",
"@types/cors": "^2.8.17",
"@types/dotenv": "^8.2.0",
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import {OrmConfig} from '../configs/db_config';
import { User } from '../entity/User';
import bcrypt from 'bcrypt';
// export all controllers
function myFunction (): void {
console.log('Hello');
Expand All @@ -26,14 +27,15 @@ const registerUser = async (req: Request, res: Response) => {
return res.status(400).json({ error: 'Email already in use' });
}

// TODO: Hash password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds);

// Create user
const user = new User();
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = password; // Replace with hashed password
user.password = hashedPassword; // Replace with hashed password
user.userType = userType;

// Save user
Expand Down
29 changes: 29 additions & 0 deletions src/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
Unique,
} from 'typeorm';

Check warning on line 7 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 7 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Entity()
@Unique(['email'])
export class User {
@PrimaryGeneratedColumn()
id!: number;

Check warning on line 13 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 13 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Column()
firstName!: string;

Check warning on line 16 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 16 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Column()
lastName!: string;

Check warning on line 19 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 19 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Column()
email!: string;

Check warning on line 22 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 22 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Column()
password!: string;

Check warning on line 25 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 25 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
@Column({ default: "User"} )
userType!: string;
}

Check warning on line 29 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

Check warning on line 29 in src/entity/User.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

0 comments on commit ad7f09a

Please sign in to comment.