-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
password encrypeds, json webtoken created, login registration code refactor, logic update
- Loading branch information
1 parent
9850fd4
commit 532fd9d
Showing
8 changed files
with
360 additions
and
148 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,10 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const generateToken = (id) => { | ||
return jwt.sign({ id }, 'sankha', { | ||
expiresIn: '30d' | ||
}); // process.env.JWT_SECRET later | ||
}; | ||
module.exports = generateToken; | ||
|
||
|
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,74 @@ | ||
const express = require('express'); | ||
const User = require("../models/userModel"); | ||
const asyncHandler = require('express-async-handler'); | ||
const generateToken = require('../config/generateToken'); | ||
|
||
//login | ||
const loginController = asyncHandler(async (req, res) => { | ||
const { name, password } = req.body; | ||
|
||
const user = await User.findOne({ name: name }); | ||
|
||
if (user && (await user.matchPassword(password))) { | ||
const respose = { | ||
stat: "Success", | ||
_id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
isAdmin: user.isAdmin, | ||
token: generateToken(user._id), | ||
}; | ||
res.json(respose) | ||
} else { | ||
res.status(401); | ||
throw new Error('Invalid name or password or both'); | ||
} | ||
|
||
}); | ||
|
||
|
||
//registration | ||
const registerController = asyncHandler(async (req, res) => { | ||
const { name, email, password } = req.body; | ||
|
||
//check for all fields | ||
if (!name || !email || !password) { | ||
res.send(400); | ||
throw Error("all fields are not filled"); | ||
} | ||
|
||
//pre existing user | ||
const userExists = await User.findOne({ email: email }); | ||
if (userExists) { | ||
throw new Error("User already exists"); | ||
} | ||
|
||
//username already taken | ||
const usernameExists = await User.findOne({ name: name }); | ||
if (usernameExists) { | ||
throw new Error("Username already taken"); | ||
} | ||
|
||
//create user | ||
const newuser = await User.create({ | ||
name, | ||
email, | ||
password, | ||
}) | ||
|
||
if (newuser) { | ||
res.status(201).json({ | ||
_id: newuser._id, | ||
name: newuser.name, | ||
email: newuser.email, | ||
isAdmin: newuser.isAdmin, | ||
token: generateToken(newuser._id), | ||
}); | ||
} | ||
else { | ||
res.status(400); | ||
throw new Error('Registration Error'); | ||
} | ||
}); | ||
|
||
module.exports = { loginController, registerController }; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.