Skip to content

Commit

Permalink
Merge pull request #356 from agarwalhimanshugaya/main
Browse files Browse the repository at this point in the history
change the folder structure
  • Loading branch information
SUGAM-ARORA authored Jun 22, 2024
2 parents 00efb8e + 86f08d6 commit 14d37a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
38 changes: 38 additions & 0 deletions BACKEND/controllers/authcontrollers.js
Original file line number Diff line number Diff line change
@@ -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};
51 changes: 11 additions & 40 deletions BACKEND/routes/auth.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 14d37a9

Please sign in to comment.