-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from PreciousIfeaka/feat/36-user_authentication
[FEAT]: add user registration auth
- Loading branch information
Showing
23 changed files
with
8,242 additions
and
75 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,5 +1,5 @@ | ||
port=8000 | ||
auth-secret= | ||
AUTH_SECRET= | ||
DB_USER= | ||
DB_HOST= | ||
DB_PASSWORD= | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 +1,51 @@ | ||
{ | ||
"name": "hng_boilerplate_expressjs", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index", | ||
"start": "ts-node src/index.ts", | ||
"test": "jest", | ||
"typeorm": "typeorm-ts-node-commonjs" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/express": "^4.17.21", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^16.11.10", | ||
"@types/supertest": "^6.0.2", | ||
"ts-node": "10.9.1", | ||
"typescript": "4.5.2" | ||
}, | ||
"dependencies": { | ||
"@types/bcryptjs": "^2.4.6", | ||
"@types/config": "^3.3.4", | ||
"bcryptjs": "^2.4.3", | ||
"class-validator": "^0.14.1", | ||
"config": "^3.3.12", | ||
"dayjs": "^1.11.12", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.19.2", | ||
"jest": "^29.7.0", | ||
"pg": "^8.4.0", | ||
"pino": "^9.3.1", | ||
"pino-pretty": "^11.2.1", | ||
"reflect-metadata": "^0.1.13", | ||
"supertest": "^7.0.0", | ||
"ts-jest": "^29.2.3", | ||
"ts-node-dev": "^2.0.0", | ||
"typeorm": "0.3.20" | ||
} | ||
"name": "hng_boilerplate_expressjs", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start:dev": "ts-node-dev --respawn --transpile-only ./src/index", | ||
"start": "ts-node --transpile-only src/index.ts", | ||
"test": "jest", | ||
"typeorm": "typeorm-ts-node-commonjs", | ||
"build": "tsc", | ||
"prod": "node dist/index.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/express": "^4.17.21", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^16.18.103", | ||
"@types/supertest": "^6.0.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "4.5.2" | ||
}, | ||
"dependencies": { | ||
"@types/bcryptjs": "^2.4.6", | ||
"@types/config": "^3.3.4", | ||
"@types/cors": "^2.8.17", | ||
"@types/jsonwebtoken": "^9.0.6", | ||
"@types/nodemailer": "^6.4.15", | ||
"bcryptjs": "^2.4.3", | ||
"class-validator": "^0.14.1", | ||
"config": "^3.3.12", | ||
"cors": "^2.8.5", | ||
"dayjs": "^1.11.12", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.19.2", | ||
"handlebars": "^4.7.8", | ||
"jest": "^29.7.0", | ||
"jsonwebtoken": "^9.0.2", | ||
"node-mailer": "^0.1.1", | ||
"pg": "^8.12.0", | ||
"pino": "^9.3.1", | ||
"pino-pretty": "^11.2.1", | ||
"reflect-metadata": "^0.1.14", | ||
"supertest": "^7.0.0", | ||
"ts-jest": "^29.2.3", | ||
"ts-node-dev": "^2.0.0", | ||
"typeorm": "^0.3.20" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
import { AuthService } from "../services"; | ||
|
||
/* | ||
*TODO: add validation | ||
*/ | ||
const authService = new AuthService(); | ||
const signUp = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { mailSent, newUser, access_token } = await authService.signUp( | ||
req.body | ||
); | ||
res.status(201).json({ mailSent, newUser, access_token }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
const verifyOtp = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { otp, token } = req.body; | ||
const { message } = await authService.verifyEmail(token as string, otp); | ||
res.status(200).json({ message }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export { signUp, verifyOtp }; |
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 +1,3 @@ | ||
// silence is golden | ||
export * from "./AuthController"; | ||
export * from "./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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum UserRole { | ||
SUPER_ADMIN = 'super_admin', | ||
ADMIN = 'admin', | ||
USER = 'user', | ||
} |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
class HttpError extends Error { | ||
status: number; | ||
success: boolean = false; | ||
|
||
constructor(statusCode: number, message: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.status = statusCode; | ||
} | ||
} | ||
|
||
class BadRequest extends HttpError { | ||
constructor(message: string) { | ||
super(400, message); | ||
} | ||
} | ||
|
||
class ResourceNotFound extends HttpError { | ||
constructor(message: string) { | ||
super(404, message); | ||
} | ||
} | ||
|
||
class Unauthorized extends HttpError { | ||
constructor(message: string) { | ||
super(401, message); | ||
} | ||
} | ||
|
||
class Forbidden extends HttpError { | ||
constructor(message: string) { | ||
super(403, message); | ||
} | ||
} | ||
|
||
class Conflict extends HttpError { | ||
constructor(message: string) { | ||
super(409, message); | ||
} | ||
} | ||
|
||
class InvalidInput extends HttpError { | ||
constructor(message: string) { | ||
super(422, message); | ||
} | ||
} | ||
|
||
class ServerError extends HttpError { | ||
constructor(message: string) { | ||
super(500, message); | ||
} | ||
} | ||
|
||
const routeNotFound = (req: Request, res: Response, next: NextFunction) => { | ||
const message = `Route not found: ${req.originalUrl}`; | ||
res.status(404).json({ success: false, status: 404, message }); | ||
}; | ||
|
||
const errorHandler = ( | ||
err: HttpError, | ||
_req: Request, | ||
res: Response, | ||
_next: NextFunction | ||
) => { | ||
const { success, status, message } = err; | ||
const cleanedMessage = message.replace(/"/g, ""); | ||
res.status(status).json({ | ||
success, | ||
status, | ||
message: cleanedMessage, | ||
}); | ||
}; | ||
|
||
export { | ||
ServerError, | ||
Conflict, | ||
Forbidden, | ||
Unauthorized, | ||
ResourceNotFound, | ||
BadRequest, | ||
InvalidInput, | ||
HttpError, | ||
routeNotFound, | ||
errorHandler, | ||
}; |
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 +1 @@ | ||
// silence is golden | ||
export * from "./error"; |
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
Oops, something went wrong.