-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00efb8e
commit 86f08d6
Showing
2 changed files
with
49 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |