-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ameynaik-9/main
createuser and login functionality created
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 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,92 @@ | ||
require("dotenv").config(); | ||
const express = require('express'); | ||
const User = require('../models/User'); | ||
const router = express.Router(); | ||
const { body, validationResult } = require('express-validator'); | ||
const bcrypt = require('bcryptjs'); | ||
var jwt = require('jsonwebtoken'); | ||
const JWT_SECRET = "SNTCSynergyCell007"; | ||
|
||
// Create a User using: POST "/createuser". No login required | ||
router.post('/createuser', [ | ||
body('name', 'Enter a valid name').isLength({ min: 3 }), | ||
body('username', 'Enter a valid username'), | ||
body('password', 'Password must be atleast 5 characters').isLength({ min: 5 }), | ||
], async (req, res) => { | ||
let success = false; | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({success, errors: errors.array() }); | ||
} | ||
try { | ||
let user = await User.findOne({ username: req.body.username }); | ||
if (user) { | ||
return res.status(400).json({success, error: "Sorry a user with this username already exists" }) | ||
} | ||
const salt = await bcrypt.genSalt(10); | ||
const secPass = await bcrypt.hash(req.body.password, salt); | ||
|
||
// Create a new user | ||
user = await User.create({ | ||
name: req.body.name, | ||
password: secPass, | ||
username: req.body.username, | ||
}); | ||
const data = { | ||
user: { | ||
id: user.id | ||
} | ||
} | ||
const authtoken = jwt.sign(data, JWT_SECRET); | ||
// res.json(user) | ||
success=true;//yaha tak aagya matlb sab sahi hi hoga | ||
res.json({success, authtoken }) | ||
} catch (error) { | ||
console.error(error.message); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}) | ||
|
||
// Authenticate a User using: POST "/api/auth/login". No login required | ||
router.post('/login', [ | ||
body('username', 'Enter a valid username').exists(), | ||
body('password', 'Password cannot be blank').exists(), | ||
], async (req, res) => { | ||
let success = false; | ||
// If there are errors, return Bad request and the errors | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
// Here we used destructuring to pull out the username and password from req.body | ||
const { username, password } = req.body; | ||
|
||
// using try catch and promises | ||
try { | ||
let user = await User.findOne({ username }); | ||
if (!user) { | ||
success=false; | ||
return res.status(400).json({success, error: "Please try to login with correct credentials" }); | ||
} | ||
|
||
// Internally matches the hash of inputed pwd | ||
const passwordCompare = await bcrypt.compare(password, user.password); | ||
if (!passwordCompare) { | ||
success=false; | ||
return res.status(400).json({ success,error: "Please try to login with correct credentials" }); | ||
} | ||
|
||
const data = { | ||
user: { | ||
id: user.id | ||
} | ||
} | ||
const authtoken = jwt.sign(data, JWT_SECRET); | ||
success=true; | ||
res.json({ success,authtoken }) | ||
|
||
} catch (error) { | ||
console.error(error.message); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}) |