This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat📌: Created user registeration & login routes🚀
- Loading branch information
Showing
6 changed files
with
234 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,58 @@ | ||
const ErrorResponse = require('../utils/errorResponse'); | ||
const asyncHandler = require('../middleware/async'); | ||
const User = require('../models/User'); | ||
|
||
/** | ||
* @desc Register user | ||
* @route POST /api/v1/auth/register | ||
* @access Public | ||
*/ | ||
exports.register = asyncHandler(async (req, res, next) => { | ||
const { name, email, password, role } = req.body; | ||
|
||
// Create user | ||
const user = await User.create({ name, email, password, role }); | ||
|
||
sendTokenCookieResponse(user, 200, res) | ||
}) | ||
|
||
/** | ||
* @desc Log user in | ||
* @route POST /api/v1/auth/login | ||
* @access Public | ||
*/ | ||
exports.login = asyncHandler(async (req, res, next) => { | ||
const { email, password } = req.body; | ||
|
||
if (!email || !password) { | ||
return next(new ErrorResponse('Please enter an email address & a password')) | ||
} | ||
|
||
const user = await User.findOne({ email }).select('+password'); | ||
|
||
if (!user) return next(new ErrorResponse('Invalid credentials'), 401); | ||
|
||
// Check if password matches | ||
const isMatch = await user.comparePassword(password); | ||
|
||
if (!isMatch) return next(new ErrorResponse('Invalid credentials'), 401); | ||
|
||
sendTokenCookieResponse(user, 200, res) | ||
}) | ||
|
||
const sendTokenCookieResponse = (user, statusCode, res) => { | ||
// Create token | ||
const token = user.getSignedToken(); | ||
|
||
const options = { | ||
expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRE * 24 * 60 * 60 * 1000), | ||
httpOnly: true | ||
} | ||
|
||
if (process.env.NODE_ENV === 'production') options.secure = true | ||
|
||
res | ||
.status(statusCode) | ||
.cookie('token', token, options) | ||
.json({ success: true, token }); | ||
} |
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,56 @@ | ||
const mongoose = require('mongoose'); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: [true, 'Please add a name'] | ||
}, | ||
email: { | ||
type: String, | ||
required: [true, 'Please add an email'], | ||
unique: true, | ||
match: [ | ||
/\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/i, | ||
'Please enter a valid email' | ||
] | ||
}, | ||
role: { | ||
type: String, | ||
enum: ['user'], | ||
default: 'user' | ||
}, | ||
password: { | ||
type: String, | ||
required: [true, 'Please add a password'], | ||
minlength: 6, | ||
select: false | ||
}, | ||
resetPasswordToken: String, | ||
resetPasswordExpiration: Date, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now() | ||
} | ||
}) | ||
|
||
// Encrypt password using bcrypt | ||
UserSchema.pre('save', async function(next) { | ||
const salt = await bcrypt.genSalt(10); | ||
this.password = await bcrypt.hash(this.password, salt); | ||
}) | ||
|
||
// Sign JSON web token & return | ||
UserSchema.methods.getSignedToken = function () { | ||
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, { | ||
expiresIn: process.env.JWT_EXPIRE | ||
}) | ||
} | ||
|
||
// Match user entered password to hashed password | ||
UserSchema.methods.comparePassword = async function(enteredPassword) { | ||
return await bcrypt.compare(enteredPassword, this.password) | ||
} | ||
|
||
module.exports = mongoose.model('User', UserSchema); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const router = require('express').Router(); | ||
const { register, login } = require('../controllers/auth'); | ||
|
||
router.post('/register', register); | ||
router.post('/login', login); | ||
|
||
module.exports = router; |
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