From 58a16a5f3c8bf28baf2a92010ca5190b1192de58 Mon Sep 17 00:00:00 2001 From: SohamMhatre09 Date: Thu, 4 Apr 2024 23:47:08 +0530 Subject: [PATCH] made changes --- node_modules/.package-lock.json | 3 +- package-lock.json | 5 ++- package.json | 3 +- routes/admin.js | 64 --------------------------------- routes/user.js | 1 - 5 files changed, 7 insertions(+), 69 deletions(-) delete mode 100644 routes/admin.js diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 410f5e9..6c3fc2b 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -1,5 +1,6 @@ { - "name": "server", + "name": "authentication-backend", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package-lock.json b/package-lock.json index b8c8d67..1411a06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,12 @@ { - "name": "server", + "name": "authentication-backend", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { + "name": "authentication-backend", + "version": "1.0.0", "dependencies": { "bcrypt": "^5.1.1", "cors": "^2.8.5", diff --git a/package.json b/package.json index 3c894bf..e2c39ff 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,9 @@ "description": "Backend App", "main": "./index.js", "scripts": { - "start": "node ./index.js" + "start": "Node ./index.js" }, "dependencies": { - "bcrypt": "^5.1.1", "cors": "^2.8.5", "express": "^4.19.2", "jsonwebtoken": "^9.0.2", diff --git a/routes/admin.js b/routes/admin.js deleted file mode 100644 index 8b55dcc..0000000 --- a/routes/admin.js +++ /dev/null @@ -1,64 +0,0 @@ -const jwt = require('jsonwebtoken'); -const express = require('express'); -const bcrypt = require('bcrypt'); -const { authenticateJwt, SECRET } = require("../middleware/auth"); -const { Admin } = require("../db"); -const router = express.Router(); -// Signup route -router.post('/signup', async (req, res) => { - const { username, password } = req.body; - try { - // Check if username already exists - const existingUser = await Admin.findOne({ username }); - if (existingUser) { - return res.status(400).json({ message: 'Username already exists' }); - } - // Create a new admin user - const newUser = new Admin({ username, password }); - await newUser.save(); - res.status(201).json({ message: 'Admin user created successfully' }); - } catch (error) { - console.error(error); - res.status(500).json({ message: 'Error creating admin user' }); - } -}); - -router.post('/login', async (req, res) => { - const { username, password } = req.body; - try { - const user = await Admin.findOne({ username }); - if (user) { - // Compare passwords directly without hashing - if (password === user.password) { - const token = jwt.sign({ username }, SECRET, { expiresIn: '1h' }); - return res.json({ message: 'Logged in successfully', token }); - } else { - return res.status(403).json({ message: 'Invalid username or password' }); - } - } else { - return res.status(403).json({ message: 'Invalid username or password' }); - } - } catch (error) { - console.error(error); - return res.status(500).json({ message: 'Error logging in' }); - } -}); - - -router.get("/me", authenticateJwt, async (req, res) => { - try { - const user = await Admin.findOne({ username: req.user.username }); - if (!user) { - return res.status(403).json({ message: "User not found" }); - } - res.json({ - name: user.username, - id: user._id - }); - } catch (error) { - console.error(error); - res.status(500).json({ message: 'Error retrieving user' }); - } -}); - -module.exports = router; diff --git a/routes/user.js b/routes/user.js index d6c1407..5dd6c74 100644 --- a/routes/user.js +++ b/routes/user.js @@ -1,4 +1,3 @@ - const jwt = require('jsonwebtoken'); const express = require('express');