-
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.
- Loading branch information
Showing
9 changed files
with
159 additions
and
19 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/node_modules | ||
|
||
package-lock.json | ||
default.json | ||
.env |
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,88 @@ | ||
const express = require('express'); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
const dotenv = require('dotenv').config(); | ||
|
||
const User = require('../models/User'); | ||
|
||
exports.register = async (req, res) => { | ||
const {name, email, password } = req.body; | ||
|
||
try { | ||
let user = await User.findOne({ email }); | ||
|
||
if (user) { | ||
return res.status(400).json({ errors: [{ message: 'User already exists' }] }); | ||
} | ||
|
||
const avatar = gravatar.url(email, { | ||
s: '200', | ||
r: 'pg', | ||
d: 'mm' | ||
}); | ||
|
||
user = new User({ name, email, avatar, password }); | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
user.password = await bcrypt.hash(password, salt); | ||
|
||
await user.save(); | ||
|
||
const payload = { | ||
user: { | ||
id: user.id | ||
} | ||
} | ||
|
||
jwt.sign( | ||
payload, | ||
process.env.jwtSecret, | ||
{expiresIn: 36000}, | ||
(err, token) => { | ||
if (err) throw err; | ||
res.json({ token }); | ||
}); | ||
|
||
|
||
} catch(err) { | ||
console.log(err.message); | ||
res.status(500).send('Server error'); | ||
} | ||
}; | ||
|
||
exports.login = async (req, res) => { | ||
const {email, password } = req.body; | ||
|
||
try { | ||
let user = await User.findOne({ email }); | ||
|
||
if (!user) { | ||
return res.status(400).json({ errors: [{ message: 'Invalid Credentials' }] }); | ||
} | ||
|
||
const isMatched = await bcrypt.compare(password, user.password); | ||
|
||
if (!isMatched) { | ||
return res.status(400).json({ errors: [{ message: 'Invalid Credentials' }] }); | ||
} | ||
|
||
const payload = { | ||
user: { | ||
id: user.id | ||
} | ||
} | ||
|
||
jwt.sign( | ||
payload, | ||
process.env.jwtSecret, | ||
{expiresIn: 36000}, | ||
(err, token) => { | ||
if (err) throw err; | ||
res.json({ token }); | ||
}); | ||
|
||
} catch(err) { | ||
console.log(err.message); | ||
res.status(500).send('Server error'); | ||
} | ||
}; |
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,18 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const dotenv = require('dotenv').config(); | ||
|
||
module.exports = (req, res, next) => { | ||
const token = req.header('x-auth-token'); | ||
|
||
if (!token) { | ||
return res.status(401).json({ message: 'No token, authorization denied' }); | ||
} | ||
|
||
try { | ||
const decoded = jwt.verify(token, process.env.jwtSecret); | ||
req.user = decoded.user; | ||
next(); | ||
} catch(err) { | ||
res.status(401).json({ message: 'Token is not valid' }); | ||
} | ||
} |
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,29 @@ | ||
const { check, validationResult } = require('express-validator'); | ||
|
||
exports.login = [ | ||
check('email', 'Please include a valid email').isEmail(), | ||
check('password', 'Password id required').exists(), | ||
|
||
function(req, res, next) { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
]; | ||
|
||
exports.register = [ | ||
check('name', 'Name is required').not().isEmpty(), | ||
check('email', 'Please include a valid email').isEmail(), | ||
check('password', 'Please enter a password with 6 or more characters').isLength({ min: 6 }), | ||
|
||
function(req, res, next) { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
return next(); | ||
} | ||
]; | ||
|
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const authCtrl = require('../controllers/authController'); | ||
const validation = require('../middleware/validations/authValidation'); | ||
|
||
router.post('/login', validation.login, authCtrl.login); | ||
router.post('/register', validation.register, authCtrl.register); | ||
|
||
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