From 86f08d6b9688aff7b31eb1b0e1db86732610ec4a Mon Sep 17 00:00:00 2001 From: Himanshu Kumar Date: Sat, 22 Jun 2024 03:40:52 +0530 Subject: [PATCH] change the folder structure --- BACKEND/controllers/authcontrollers.js | 38 +++++++++++++++++++ BACKEND/routes/auth.js | 51 ++++++-------------------- 2 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 BACKEND/controllers/authcontrollers.js 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