From 6e9d219532cfb09f036f56565ece3f17be7b7475 Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sat, 2 Mar 2024 19:34:50 +0500 Subject: [PATCH] refactor: user routes and controllers --- apps/api/src/controllers/user_controller.ts | 32 ++++++--------------- apps/api/src/models/user_model.ts | 2 +- apps/api/src/routes/user_route.ts | 18 ++++++------ 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/apps/api/src/controllers/user_controller.ts b/apps/api/src/controllers/user_controller.ts index e369167..8dba93b 100644 --- a/apps/api/src/controllers/user_controller.ts +++ b/apps/api/src/controllers/user_controller.ts @@ -7,7 +7,7 @@ import { generate_token } from 'utils/generate_token' //* @desc Post user //* route POST /api/user //? @access Public -export async function post_user(req: Request, res: Response): Promise { +export async function create_user(req: Request, res: Response): Promise { try { const user_data = req.body // check if user email already exists @@ -28,10 +28,10 @@ export async function post_user(req: Request, res: Response): Promise { } } -//* @desc Get user +//* @desc Login user //* route GET /api/user //! @access Public -export async function get_user(req: Request, res: Response): Promise { +export async function login_user(req: Request, res: Response): Promise { try { //* get user by email and password const { email, password } = req.body @@ -50,28 +50,14 @@ export async function get_user(req: Request, res: Response): Promise { } } -//* @desc Delete user -//* route DELETE /api/user/:id -//! @access Private -export async function delete_user(req: Request, res: Response): Promise { +//* @desc Logout user +//* route POST /api/user/logout +// ? @access Public +export async function logout_user(_: Request, res: Response): Promise { try { - const { id } = req.params - //* check if id is valid - if (!isValidObjectId(id)) { - res.status(400).json({ error: 'Invalid id' }) - return - } - //* check if user exists - const user = await USER_SCHEMA.findById(id) - if (!user) { - res.status(404).json({ error: 'user not found' }) - return - } - //* delete user - await USER_SCHEMA.findByIdAndDelete(id) - res.status(200).json({ message: 'user deleted successfully' }) + res.clearCookie('token').status(200).json({ message: 'User logged out' }) } catch (error) { - log('Error deleting user:', error) + log('Error logging out user:', error) res.status(500).json({ error: 'Internal server error' }) } } diff --git a/apps/api/src/models/user_model.ts b/apps/api/src/models/user_model.ts index 4a218c2..b65a5ae 100644 --- a/apps/api/src/models/user_model.ts +++ b/apps/api/src/models/user_model.ts @@ -2,9 +2,9 @@ import mongoose, { Document, Schema } from 'mongoose' import bcrypt from 'bcrypt' interface IUserSchema extends Document { + name?: string email: string password: string - name?: string match_password: (password: string) => Promise } diff --git a/apps/api/src/routes/user_route.ts b/apps/api/src/routes/user_route.ts index 7cb4c1f..1a7b8d5 100644 --- a/apps/api/src/routes/user_route.ts +++ b/apps/api/src/routes/user_route.ts @@ -3,23 +3,23 @@ const router = express.Router() import { validate_schema } from '@/middleware/validation_middleware' import { USER_VALIDATION_SCHEMA } from '@/validations/user_validation' import { - delete_user, - get_user, - post_user, + login_user, + logout_user, + create_user, update_user, } from '@/controllers/user_controller' //* @desc Post user //? @access Public -router.post('/', validate_schema(USER_VALIDATION_SCHEMA), post_user) +router.post('/', validate_schema(USER_VALIDATION_SCHEMA), create_user) -//* @desc Get user +//* @desc Login user //? @access Public -router.get('/', validate_schema(USER_VALIDATION_SCHEMA), get_user) +router.get('/', validate_schema(USER_VALIDATION_SCHEMA), login_user) -//* @desc Delete user -//! @access Private -router.delete('/:id', delete_user) +//* @desc Logout user +//? @access Public +router.post('/logout', logout_user) //* @desc Update user //! @access Private