diff --git a/backend/src/controllers/authControllers.ts b/backend/src/controllers/authControllers.ts deleted file mode 100644 index 1794c80..0000000 --- a/backend/src/controllers/authControllers.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { Request, Response } from 'express'; -import User from '../models/userModels'; -import { catchError } from '../utils'; - -export const register = catchError( - async (req: Request, res: Response): Promise => { - const { username, email, password } = req.body; - - const userExist = await User.findOne({ email }); - if (userExist) { - res.status(400).json({ msg: 'Email Already Exists' }); - return; - } - - const userCreated = new User({ - username, - email, - password, - }); - - await userCreated.save(); - - res.status(201).json({ - msg: 'Registration Successful', - token: await userCreated.generateToken(), - userId: userCreated._id.toString(), - }); - } -); - -export const login = catchError( - async (req: Request, res: Response): Promise => { - const { email, password } = req.body; - const userExist = await User.findOne({ email }); - - if (!userExist) { - res.status(400).json({ message: 'Invalid Credentials' }); - return; - } - - const isPasswordMatch = await userExist.comparePassword(password); - - if (isPasswordMatch) { - res.status(200).json({ - msg: 'Login Successful', - token: await userExist.generateToken(), - userId: userExist._id.toString(), - }); - } else { - res.status(401).json({ message: 'Invalid Email or Password' }); - } - } -); diff --git a/backend/src/index.ts b/backend/src/index.ts index a241fec..7e8256b 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -3,7 +3,6 @@ import express, { json } from 'express'; import { connect } from 'mongoose'; import cors from 'cors'; import { config } from 'dotenv'; -import userRoutes from './routes/userRoutes'; config(); @@ -21,8 +20,6 @@ app.get('/', (req, res) => { res.send('Hello from the backend!'); }); -app.use('/api/v1/auth', userRoutes); - app.listen(port, () => { console.log(`Server running on port ${port}`); -}); +}); \ No newline at end of file diff --git a/backend/src/middlewares/authMiddlewares.ts b/backend/src/middlewares/authMiddlewares.ts deleted file mode 100644 index 0b28acf..0000000 --- a/backend/src/middlewares/authMiddlewares.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Request, Response, NextFunction } from 'express'; -import jwt from 'jsonwebtoken'; -import User, { IUser } from '../models/userModels'; - -export interface AuthenticatedRequest extends Request { - user?: IUser; -} - -export const authMiddleware = async ( - req: AuthenticatedRequest, - res: Response, - next: NextFunction -) => { - const token = req.header('Authorization')?.replace('Bearer ', ''); - - if (!token) { - return res - .status(401) - .json({ message: 'No token provided, authorization denied' }); - } - - try { - const decoded = jwt.verify(token, process.env.JWT_SECRET as string) as { - userId: string; - }; - const user = await User.findById(decoded.userId); - - if (!user) { - return res - .status(401) - .json({ message: 'User not found, authorization denied' }); - } - - req.user = user as IUser; - next(); - } catch (error) { - console.error(error); - res.status(401).json({ - message: 'Invalid token, authorization denied', - }); - } -}; diff --git a/backend/src/models/userModels.ts b/backend/src/models/userModels.ts deleted file mode 100644 index 26a6780..0000000 --- a/backend/src/models/userModels.ts +++ /dev/null @@ -1,70 +0,0 @@ -import mongoose, { Document, Model } from 'mongoose'; -import bcrypt from 'bcrypt'; -import jwt from 'jsonwebtoken'; - -export interface IUser extends Document { - username: string; - email: string; - password: string; - _id: mongoose.Types.ObjectId; - comparePassword(password: string): Promise; - generateToken(): Promise; -} - -const userSchema = new mongoose.Schema({ - username: { - type: String, - required: true, - }, - email: { - type: String, - required: true, - }, - password: { - type: String, - required: true, - }, -}); - -userSchema.pre('save', async function (next) { - const user = this as IUser; - if (!user.isModified('password')) { - return next(); - } - - try { - const saltRounds = await bcrypt.genSalt(10); - const hashedPassword = await bcrypt.hash(user.password, saltRounds); - user.password = hashedPassword; - next(); - } catch (error) { - next(error as Error); - } -}); - -userSchema.methods.comparePassword = async function ( - password: string -): Promise { - return bcrypt.compare(password, this.password); -}; - -userSchema.methods.generateToken = async function (): Promise { - try { - return jwt.sign( - { - userId: this._id.toString(), - email: this.email, - }, - process.env.JWT_SECRET as string, - { - expiresIn: '30d', - } - ); - } catch (error) { - console.error(error); - throw new Error('Token generation failed'); - } -}; - -const User: Model = mongoose.model('User', userSchema); -export default User; diff --git a/backend/src/routes/userRoutes.js b/backend/src/routes/userRoutes.js deleted file mode 100644 index fd9fb8b..0000000 --- a/backend/src/routes/userRoutes.js +++ /dev/null @@ -1,9 +0,0 @@ -import express from 'express'; -import { login, register } from '../controllers/authControllers'; - -const router = express.Router(); - -router.route('/register').post(register); -router.route('/login').post(login); - -export default router;