Skip to content

Commit

Permalink
Merge pull request #4 from ameynaik-9/main
Browse files Browse the repository at this point in the history
createuser and login functionality created
  • Loading branch information
shubham21155102 authored Dec 20, 2022
2 parents 3db7b1d + 1ebb642 commit 294be98
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions routes/auth.js
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");
}
})

0 comments on commit 294be98

Please sign in to comment.