diff --git a/BACKEND/controllers/authcontrollers.js b/BACKEND/controllers/authcontrollers.js new file mode 100644 index 00000000..1081ada2 --- /dev/null +++ b/BACKEND/controllers/authcontrollers.js @@ -0,0 +1,38 @@ +const asyncHandler = require("express-async-handler"); +const User = require('../models/user'); +const jwt = require('jsonwebtoken'); + +const signup = asyncHandler(async (req, res) => { + const { username, email, password } = req.body; + try { + const user = new User({ username, email, password }); + await user.save(); + res.status(201).send('User created successfully'); + } catch (error) { + res.status(400).send(error); + } + }); + + const loginuser = asyncHandler(async (req, res) => { + const { email, password } = req.body; + try { + const user = await User.findOne({ email }); + if (!user) return res.status(401).send('Invalid email or password'); + + const isMatch = await user.comparePassword(password); + if (!isMatch) return res.status(401).send('Invalid email or password'); + + const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' }); + res.cookie('token', token, { httpOnly: true }); + res.send('Logged in successfully'); + } catch (error) { + res.status(400).send(error); + } + }); + + const logoutuser = asyncHandler(async (req, res) => { + res.clearCookie('token'); + res.send('Logged out successfully'); + }); + +module.exports = {signup,loginuser,logoutuser}; \ No newline at end of file diff --git a/BACKEND/routes/auth.js b/BACKEND/routes/auth.js index 77dcaff1..33aaa1b7 100644 --- a/BACKEND/routes/auth.js +++ b/BACKEND/routes/auth.js @@ -1,40 +1,11 @@ -const express = require('express'); -const router = express.Router(); -const User = require('../models/user'); -const jwt = require('jsonwebtoken'); -const bcrypt = require('bcryptjs'); - -router.post('/signup', async (req, res) => { - const { username, email, password } = req.body; - try { - const user = new User({ username, email, password }); - await user.save(); - res.status(201).send('User created successfully'); - } catch (error) { - res.status(400).send(error); - } -}); - -router.post('/login', async (req, res) => { - const { email, password } = req.body; - try { - const user = await User.findOne({ email }); - if (!user) return res.status(401).send('Invalid email or password'); - - const isMatch = await user.comparePassword(password); - if (!isMatch) return res.status(401).send('Invalid email or password'); - - const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' }); - res.cookie('token', token, { httpOnly: true }); - res.send('Logged in successfully'); - } catch (error) { - res.status(400).send(error); - } -}); - -router.post('/logout', (req, res) => { - res.clearCookie('token'); - res.send('Logged out successfully'); -}); - -module.exports = router; +const express=require("express"); +const { + signup, + loginuser, + logoutuser +}=require("../controllers/authcontrollers.js"); +const router=express.Router(); +router.get("/signup",signup); +router.get("/login",loginuser); +router.get("/logout",logoutuser); +module.exports=router; \ No newline at end of file