-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
672 additions
and
170 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
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,163 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import ApiResponse from '../utils/handlers/api-reponse'; | ||
import { ErrorResponseType } from '../utils/types'; | ||
import AuthService from '../services/auth.service'; | ||
|
||
class AuthController { | ||
static async register( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.register(req.body); | ||
if (response.success) { | ||
ApiResponse.success(res, response, 201); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async verifyAccount( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.verifyAccount(req.body); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async loginWithPassword( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.loginWithPassword(req.body); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async generateLoginOtp( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.generateLoginOtp(req.body.email); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async loginWithOtp( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.loginWithOtp(req.body); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async refreshToken( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.refresh(req.body.refreshToken); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async logout( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const { accessToken, refreshToken } = req.body; | ||
const response = await AuthService.logout(accessToken, refreshToken); | ||
if (response.success) { | ||
ApiResponse.success(res, response, 202); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async forgotPassword( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.forgotPassword(req.body.email); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
|
||
static async resetPassword( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
): Promise<void> { | ||
try { | ||
const response = await AuthService.resetPassword(req.body); | ||
if (response.success) { | ||
ApiResponse.success(res, response); | ||
} else { | ||
throw response; | ||
} | ||
} catch (error) { | ||
ApiResponse.error(res, error as ErrorResponseType); | ||
} | ||
} | ||
} | ||
|
||
export default AuthController; |
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,16 @@ | ||
import { Router } from 'express'; | ||
import AuthController from '../controllers/auth.controller'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/register', AuthController.register); | ||
router.post('/verify-account', AuthController.verifyAccount); | ||
router.post('/login', AuthController.loginWithPassword); | ||
router.post('/generate-login-otp', AuthController.generateLoginOtp); | ||
router.post('/login-with-otp', AuthController.loginWithOtp); | ||
router.post('/refresh-token', AuthController.refreshToken); | ||
router.post('/logout', AuthController.logout); | ||
router.post('/forgot-password', AuthController.forgotPassword); | ||
router.patch('/reset-password', AuthController.resetPassword); | ||
|
||
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
Oops, something went wrong.