diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-25 11:07:20 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-25 11:07:20 +0530 |
commit | 1f853be5b9c07519cceeea9b7a5cbeee06756401 (patch) | |
tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /server/routes | |
parent | 90b1b7872547949b3b2c42bc7cc47457a1a2ec70 (diff) | |
download | admin-panel-1f853be5b9c07519cceeea9b7a5cbeee06756401.tar.gz admin-panel-1f853be5b9c07519cceeea9b7a5cbeee06756401.tar.bz2 admin-panel-1f853be5b9c07519cceeea9b7a5cbeee06756401.zip |
new project
Diffstat (limited to 'server/routes')
-rw-r--r-- | server/routes/auth.js | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/server/routes/auth.js b/server/routes/auth.js deleted file mode 100644 index 04a9d5b..0000000 --- a/server/routes/auth.js +++ /dev/null @@ -1,50 +0,0 @@ -const express = require('express'); -const bcrypt = require('bcryptjs'); -const jwt = require('jwt-simple'); -const User = require('../models/User'); -require('dotenv').config(); - -const router = express.Router(); -const secret = process.env.JWT_SECRET; - -// Register new user -router.post('/register', async (req, res) => { - const { email, password } = req.body; - try { - const user = await User.findOne({ email }); - if (user) return res.status(400).json({ msg: 'User already exists' }); - - const newUser = new User({ email, password }); - const salt = await bcrypt.genSalt(10); - newUser.password = await bcrypt.hash(password, salt); - await newUser.save(); - - const payload = { id: newUser.id }; - const token = jwt.encode(payload, secret); - res.json({ token }); - } catch (err) { - console.error(err.message); - res.status(500).send('Server error'); - } -}); - -// Login user -router.post('/login', async (req, res) => { - const { email, password } = req.body; - try { - const user = await User.findOne({ email }); - if (!user) return res.status(400).json({ msg: 'Invalid credentials' }); - - const isMatch = await bcrypt.compare(password, user.password); - if (!isMatch) return res.status(400).json({ msg: 'Invalid credentials' }); - - const payload = { id: user.id }; - const token = jwt.encode(payload, secret); - res.json({ token }); - } catch (err) { - console.error(err.message); - res.status(500).send('Server error'); - } -}); - -module.exports = router; |