Skip to content

Commit

Permalink
Final Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ukataria committed Feb 9, 2024
1 parent 6ee3c39 commit 2b4079b
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 5 deletions.
260 changes: 257 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"ipaddr.js": "^1.9.1",
"is-fullwidth-code-point": "^3.0.0",
"isarray": "^1.0.0",
"jsonwebtoken": "^9.0.2",
"kareem": "^2.5.1",
"keygrip": "^1.1.0",
"lru-cache": "^6.0.0",
Expand Down Expand Up @@ -136,8 +137,8 @@
"sparse-bitfield": "^3.0.3",
"statuses": "^2.0.1",
"streamsearch": "^1.1.0",
"string-width": "^4.2.3",
"string_decoder": "^1.3.0",
"string-width": "^4.2.3",
"strip-ansi": "^6.0.1",
"tar": "^6.2.0",
"toidentifier": "^1.0.1",
Expand All @@ -149,6 +150,7 @@
"unpipe": "^1.0.0",
"util-deprecate": "^1.0.2",
"utils-merge": "^1.0.1",
"uuid": "^9.0.1",
"vary": "^1.1.2",
"webidl-conversions": "^3.0.1",
"whatwg-url": "^5.0.0",
Expand All @@ -157,7 +159,6 @@
"xtend": "^4.0.2",
"yallist": "^4.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
Expand Down
15 changes: 15 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// routes/auth.js
const express = require('express');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
const User = require('../models/user');
const router = express.Router();

router.post('/register', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const identity = uuidv4();
const user = new User({ email: req.body.email, password: hashedPassword });
const savedUser = await user.save();

const token = jwt.sign(
{ userId: savedUser._id, email: savedUser.email },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);

res.status(201).send({ message: 'User created', userId: savedUser._id });
} catch (error) {
console.error(error);
Expand All @@ -27,6 +37,11 @@ router.post('/login', async (req, res) => {
}
try {
if (await bcrypt.compare(req.body.password, user.password)) {
const token = jwt.sign(
{ userId: user._id, email: user.email },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);
res.send('Success');
} else {
res.send('Not Allowed');
Expand Down

0 comments on commit 2b4079b

Please sign in to comment.