-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(user registration): resolve registration bug
- ensure that a user provide neccessary inputs - restructure user entity - refactor other codes depending on user registration logic [Fixes #39]
Showing
18 changed files
with
240 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
PORT= ******************************** | ||
APP_ENV= ******************************** | ||
PDN_DB_NAME= ***************************** | ||
|
||
TEST_DB_HOST= ******************************** | ||
TEST_DB_PORT= ******************************** | ||
TEST_DB_USER= ******************************** | ||
TEST_DB_PASS= ******************************** | ||
TEST_DB_NAME= ******************************** | ||
|
||
DEV_DB_HOST= ******************************** | ||
DEV_DB_PORT= ******************************** | ||
DEV_DB_USER= ******************************** | ||
DEV_DB_PASS= ***************************** | ||
DEV_DB_TYPE= ******************************* | ||
DEV_DB_NAME= ******************************* | ||
|
||
PDN_DB_HOST= ******************************** | ||
PDN_DB_PORT= ******************************** | ||
PDN_DB_USER= ******************************** | ||
PDN_DB_PASS= ******************************** | ||
PDN_DB_PASS= ******************************** | ||
PDN_DB_NAME= ***************************** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
module.exports = { | ||
"type": "postgres", | ||
"host": `${process.env.DEV_DB_HOST}`, | ||
"port": `${process.env.DEV_DB_PORT}`, | ||
"username": `${process.env.DEV_DB_USER}`, | ||
"password": `${process.env.DEV_DB_PASS}`, | ||
"database": `${process.env.DEV_DB_NAME}`, | ||
"synchronize": true, | ||
"logging": false, | ||
"entities": [ | ||
"src/entities/**/*.ts" | ||
], | ||
"migrations": [ | ||
"src/migrations/**/*.ts" | ||
], | ||
"subscribers": [ | ||
"src/subscribers/**/*.ts" | ||
], | ||
"cli": { | ||
"entitiesDir": "src/entities", | ||
"migrationsDir": "src/migrations", | ||
"subscribersDir": "src/subscribers" | ||
} | ||
}; | ||
const devConfig = { | ||
type: 'postgres', | ||
host: process.env.DEV_DB_HOST, | ||
port: process.env.DEV_DB_PORT, | ||
username: process.env.DEV_DB_USER, | ||
password: process.env.DEV_DB_PASS, | ||
database: process.env.DEV_DB_NAME, | ||
synchronize: true, | ||
logging: false, | ||
entities: ['src/entities/**/*.ts'], | ||
migrations: ['src/migrations/**/*.ts'], | ||
subscribers: ['src/subscribers/**/*.ts'], | ||
cli: { | ||
entitiesDir: 'src/entities', | ||
migrationsDir: 'src/migrations', | ||
subscribersDir: 'src/subscribers', | ||
}, | ||
}; | ||
|
||
const testConfig = { | ||
type: 'postgres', | ||
host: process.env.TEST_DB_HOST, | ||
port: process.env.TEST_DB_PORT, | ||
username: process.env.TEST_DB_USER, | ||
password: process.env.TEST_DB_PASS, | ||
database: process.env.TEST_DB_NAME, | ||
synchronize: true, | ||
logging: false, | ||
entities: ['src/entities/**/*.ts'], | ||
migrations: ['src/migrations/**/*.ts'], | ||
subscribers: ['src/subscribers/**/*.ts'], | ||
cli: { | ||
entitiesDir: 'src/entities', | ||
migrationsDir: 'src/migrations', | ||
subscribersDir: 'src/subscribers', | ||
}, | ||
}; | ||
|
||
module.exports = process.env.NODE_ENV === 'test' ? testConfig : devConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,54 @@ | ||
import { Request, Response } from 'express'; | ||
import { User } from '../entities/User';; | ||
import { User } from '../entities/User'; | ||
import bcrypt from 'bcrypt'; | ||
import { getRepository } from 'typeorm'; | ||
|
||
import { responseError, responseServerError, responseSuccess } from '../utils/response.utils'; | ||
import { validate } from 'class-validator'; | ||
Check warning on line 6 in src/controllers/authController.ts
|
||
|
||
class UserController { | ||
static registerUser = async (req: Request, res: Response) => { | ||
const { firstName, lastName, email, password, gender, phoneNumber, userType, status, verified, photoUrl } = req.body; | ||
|
||
// Validate user input | ||
if (!(firstName && lastName && email && password && gender && phoneNumber && verified && photoUrl)) { | ||
return res.status(400).json({ error: 'Please fill all the fields' }); | ||
} | ||
|
||
const userRepository = getRepository(User); | ||
|
||
|
||
// Check for existing user | ||
const existingUser = await userRepository.findOneBy({ email }); | ||
const existingUserNumber = await userRepository.findOneBy({ phoneNumber }); | ||
|
||
if (existingUser || existingUserNumber) { | ||
return res.status(400).json({ error: 'Email or phone number already in use' }); | ||
} | ||
|
||
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 = hashedPassword; | ||
user.userType = userType; | ||
user.gender = gender; | ||
user.phoneNumber = phoneNumber; | ||
user.photoUrl = photoUrl; | ||
user.status = status ? status : 'active'; | ||
user.verified = verified; | ||
|
||
// Save user | ||
await userRepository.save(user); | ||
|
||
return res.status(201).json({ message: 'User registered successfully' }); | ||
}; | ||
static registerUser = async (req: Request, res: Response) => { | ||
Check warning on line 9 in src/controllers/authController.ts
|
||
const { firstName, lastName, email, password, gender, phoneNumber, userType } = req.body; | ||
|
||
// Validate user input | ||
if (!firstName || !lastName || !email || !password || !gender || !phoneNumber) { | ||
return responseError(res, 400, 'Please fill all the required fields'); | ||
} | ||
|
||
const userRepository = getRepository(User); | ||
|
||
try { | ||
// Check for existing user | ||
const existingUser = await userRepository.findOneBy({ email }); | ||
const existingUserNumber = await userRepository.findOneBy({ phoneNumber }); | ||
|
||
if (existingUser || existingUserNumber) { | ||
return responseError(res, 409, 'Email or phone number already in use'); | ||
} | ||
|
||
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 = hashedPassword; | ||
user.userType = userType; | ||
user.gender = gender; | ||
user.phoneNumber = phoneNumber; | ||
|
||
// Save user | ||
await userRepository.save(user); | ||
|
||
return responseSuccess(res, 201, 'User registered successfully'); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
return responseServerError(res, error.message); | ||
} | ||
|
||
return responseServerError(res, 'Unknown error occurred'); | ||
} | ||
}; | ||
} | ||
export { UserController }; | ||
export { UserController }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { UserController } from './authController'; | ||
|
||
export{UserController}; | ||
export { UserController }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,62 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
Unique, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { IsEmail, IsNotEmpty, IsString, IsBoolean, IsIn } from 'class-validator'; | ||
|
||
@Entity() | ||
@Unique(['email']) | ||
export class User { | ||
@PrimaryGeneratedColumn('uuid') | ||
@IsNotEmpty() | ||
id!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
firstName!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
lastName!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsEmail() | ||
email!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
password!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
gender!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
phoneNumber!: string; | ||
|
||
@Column({ nullable: true }) | ||
photoUrl?: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsBoolean() | ||
verified!: boolean; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsIn(['active', 'suspended']) | ||
status!: 'active' | 'suspended'; | ||
|
||
@Column({ default: "Buyer" }) | ||
@IsNotEmpty() | ||
@IsIn(['Admin', 'Buyer', 'Vendor']) | ||
userType!: 'Admin' | 'Buyer' | 'Vendor'; | ||
|
||
@CreateDateColumn() | ||
createdAt!: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt!: Date; | ||
} | ||
import { Entity, PrimaryGeneratedColumn, Column, Unique, CreateDateColumn, UpdateDateColumn } from 'typeorm'; | ||
import { IsEmail, IsNotEmpty, IsString, IsBoolean, IsIn } from 'class-validator'; | ||
|
||
@Entity() | ||
@Unique(['email']) | ||
export class User { | ||
@PrimaryGeneratedColumn('uuid') | ||
@IsNotEmpty() | ||
id!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
firstName!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
lastName!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsEmail() | ||
email!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
password!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsString() | ||
gender!: string; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
phoneNumber!: string; | ||
|
||
@Column({ nullable: true }) | ||
photoUrl?: string; | ||
|
||
@Column({ default: false }) | ||
@IsNotEmpty() | ||
@IsBoolean() | ||
isVerified!: boolean; | ||
|
||
@Column({ default: 'active' }) | ||
@IsNotEmpty() | ||
@IsIn(['active', 'suspended']) | ||
status!: 'active' | 'suspended'; | ||
|
||
@Column({ default: 'Buyer' }) | ||
@IsNotEmpty() | ||
@IsIn(['Admin', 'Buyer', 'Vendor']) | ||
userType!: 'Admin' | 'Buyer' | 'Vendor'; | ||
|
||
@CreateDateColumn() | ||
createdAt!: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt!: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { Router } from 'express'; | ||
import { Router } from 'express'; | ||
import { UserController } from '../controllers/index'; | ||
|
||
|
||
const { registerUser } = UserController; | ||
|
||
const router = Router(); | ||
|
||
router.post('/register', registerUser); | ||
|
||
export default router; | ||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters