Skip to content

Commit

Permalink
fix: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hariscs committed Mar 9, 2024
1 parent 0680422 commit bd71602
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NODE_ENV=development
PORT=5000
MONGO_URI=YOUR_MONGO_UR
MONGO_URI=YOUR_MONGO_URI
JWT_SECRET=YOUR_JWT_SECRET
88 changes: 44 additions & 44 deletions apps/api/src/controllers/user_controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response } from "express";
import { USER_SCHEMA } from "@/models/user_model";
import { isValidObjectId } from "mongoose";
import { log } from "console";
import { generate_token } from "utils/generate_token";
import { Request, Response } from 'express'
import { USER_SCHEMA } from '@/models/user_model'
import { isValidObjectId } from 'mongoose'
import { log } from 'console'
import { generate_token } from 'utils/generate_token'

//* @desc Register user
//* route POST /api/user
Expand All @@ -12,22 +12,22 @@ export async function register_user(
res: Response
): Promise<void> {
try {
const user_data = req.body;
const user_data = req.body
// check if user email already exists
const { email } = req.body;
const existing_user = await USER_SCHEMA.findOne({ email });
const { email } = req.body
const existing_user = await USER_SCHEMA.findOne({ email })
if (existing_user) {
res.status(409).json({ error: "User email already exists" });
return;
res.status(409).json({ error: 'User email already exists' })
return
}

//* post user
const user = await USER_SCHEMA.create(user_data);
generate_token(res, user._id.toString());
res.status(201).json(user);
const user = await USER_SCHEMA.create(user_data)
generate_token(res, user._id.toString())
res.status(201).json(user)
} catch (error) {
log("Error posting user:", error);
res.status(500).json({ error: "Internal server error" });
log('Error posting user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}

Expand All @@ -37,19 +37,19 @@ export async function register_user(
export async function login_user(req: Request, res: Response): Promise<void> {
try {
//* get user by email and password
const { email, password } = req.body;
const user = await USER_SCHEMA.findOne({ email });
const { email, password } = req.body
const user = await USER_SCHEMA.findOne({ email })

// check if user exists and password is correct
if (!user || !(await user.match_password(password))) {
res.status(401).json({ error: "Incorrect email or password" });
return;
res.status(401).json({ error: 'Incorrect email or password' })
return
}
generate_token(res, user._id.toString());
res.status(200).json(user);
generate_token(res, user._id.toString())
res.status(200).json(user)
} catch (error) {
log("Error fetching user:", error);
res.status(500).json({ error: "Internal server error" });
log('Error fetching user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}

Expand All @@ -58,10 +58,10 @@ export async function login_user(req: Request, res: Response): Promise<void> {
// ? @access Public
export async function logout_user(_: Request, res: Response): Promise<void> {
try {
res.clearCookie("token").status(200).json({ message: "User logged out" });
res.clearCookie('token').status(200).json({ message: 'User logged out' })
} catch (error) {
log("Error logging out user:", error);
res.status(500).json({ error: "Internal server error" });
log('Error logging out user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}

Expand All @@ -70,35 +70,35 @@ export async function logout_user(_: Request, res: Response): Promise<void> {
//! @access Private
export async function update_user(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params;
const { id } = req.params
//* check if id is valid
if (!isValidObjectId(id)) {
res.status(400).json({ error: "Invalid id" });
return;
res.status(400).json({ error: 'Invalid id' })
return
}
//* check if user exists
const user = await USER_SCHEMA.findById(id);
const user = await USER_SCHEMA.findById(id)
if (!user) {
res.status(404).json({ error: "user not found" });
return;
res.status(404).json({ error: 'user not found' })
return
}

//* check if user email already exists
const { email } = req.body;
const existing_user = await USER_SCHEMA.findOne({ email });
const { email } = req.body
const existing_user = await USER_SCHEMA.findOne({ email })
if (existing_user && existing_user._id.toString() !== id) {
res.status(409).json({ error: "Email already exists" });
return;
res.status(409).json({ error: 'Email already exists' })
return
}

//* update user
const updated_user = await USER_SCHEMA.findByIdAndUpdate(id, req.body, {
new: true,
});
res.status(200).json(updated_user);
})
res.status(200).json(updated_user)
} catch (error) {
log("Error updating user:", error);
res.status(500).json({ error: "Internal server error" });
log('Error updating user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}

Expand All @@ -111,10 +111,10 @@ export async function get_user(req: Request, res: Response): Promise<void> {
name: req.user?.name,
email: req.user?.email,
_id: req.user?._id,
};
res.status(200).json(user);
}
res.status(200).json(user)
} catch (error) {
log("Error fetching user:", error);
res.status(500).json({ error: "Internal server error" });
log('Error fetching user:', error)
res.status(500).json({ error: 'Internal server error' })
}
}
30 changes: 13 additions & 17 deletions apps/api/src/routes/user_route.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
import express from "express";
const router = express.Router();
import { validate_schema } from "@/middleware/validation_middleware";
import { USER_VALIDATION_SCHEMA } from "@/validations/user_validation";
import express from 'express'
const router = express.Router()
import { validate_schema } from '@/middleware/validation_middleware'
import { USER_VALIDATION_SCHEMA } from '@/validations/user_validation'
import {
login_user,
logout_user,
register_user,
update_user,
get_user,
} from "@/controllers/user_controller";
import { protect_route } from "@/middleware/auth_middleware";
} from '@/controllers/user_controller'
import { protect_route } from '@/middleware/auth_middleware'

//* @desc Create user
//? @access Public
router.post(
"/register",
validate_schema(USER_VALIDATION_SCHEMA),
register_user
);
router.post('/register', validate_schema(USER_VALIDATION_SCHEMA), register_user)

//* @desc Login user
//? @access Public
router.post("/login", validate_schema(USER_VALIDATION_SCHEMA), login_user);
router.post('/login', validate_schema(USER_VALIDATION_SCHEMA), login_user)

//* @desc Logout user
//? @access Public
router.post("/logout", logout_user);
router.post('/logout', logout_user)

//* @desc Get User
//! @access Private
router.get("/profile", protect_route, get_user);
router.get('/profile', protect_route, get_user)

//* @desc Update user
//! @access Private
router.patch(
"/:id",
'/:id',
protect_route,
validate_schema(USER_VALIDATION_SCHEMA),
update_user
);
)

export default router;
export default router

0 comments on commit bd71602

Please sign in to comment.