This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
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 #2 from ajvanliere/project-setup
Authentication with jwt and bcrypt
- Loading branch information
Showing
9 changed files
with
188 additions
and
4 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,50 @@ | ||
const config = require('../config'); | ||
const jwt = require('jsonwebtoken'); | ||
const bcrypt = require('bcrypt'); | ||
|
||
const authService = require('../services/auth'); | ||
const userService = require('../services/user'); | ||
|
||
function login(req, res){ | ||
return authService.authenticate(req.body) | ||
.then(token => { | ||
res.send({ | ||
success: true, | ||
data: { token } | ||
}); | ||
}) | ||
.catch(err => { | ||
res.send({ | ||
success: false, | ||
message: err.message | ||
}); | ||
}) | ||
}; | ||
|
||
function register(req, res){ | ||
const login = req.body.login; | ||
return userService.getUserByLogin(req.body.login || '') | ||
.then(exists => { | ||
|
||
if (exists){ | ||
return res.send({ | ||
success: false, | ||
message: 'Registration failed. User with this email already registered.' | ||
}); | ||
} | ||
|
||
var user = { | ||
login: req.body.login, | ||
password: bcrypt.hashSync(req.body.password, config.saltRounds), | ||
name: req.body.name | ||
} | ||
|
||
return userService.addUser(user) | ||
.then(() => res.send({success: true})); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
login, | ||
register | ||
} |
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
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,46 @@ | ||
const Sequelize = require('sequelize'); | ||
const sequelize = require('../db'); | ||
|
||
const User = sequelize.define('user', { | ||
login: { | ||
type: Sequelize.STRING, | ||
isEmail: true, | ||
allowNull: false | ||
}, | ||
password: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
}, | ||
); | ||
|
||
const Score = sequelize.define('scores', { | ||
title: Sequelize.STRING, | ||
level: { | ||
type: Sequelize.STRING, | ||
}, | ||
question_correct: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
question_attempted: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: User, | ||
key: 'id' | ||
} | ||
} | ||
}); | ||
|
||
User.hasMany(Score, { foreignKey: 'user_id' }); | ||
|
||
module.exports = { | ||
User, | ||
Score | ||
} |
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,3 +1,6 @@ | ||
const authController = require('./controllers/auth'); | ||
|
||
module.exports.set = app => { | ||
//endpoints will be here soon | ||
app.post('/login', authController.login); | ||
app.post('/register', authController.register); | ||
} |
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,16 @@ | ||
const { Router } = require('express'); | ||
const User = require('../models/userModel'); | ||
// const bcrypt = require('bcrypt'); | ||
|
||
const router = new Router(); | ||
|
||
router.get('/users', (req, res, next) => { | ||
User | ||
.findAll() | ||
.then(users => { | ||
res.send({ users }) | ||
}) | ||
.catch(error => next(error)) | ||
}) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const bcrypt = require('bcrypt'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const Users = require('../models').User; | ||
const config = require('../config'); | ||
|
||
const authenticate = params => { | ||
return Users.findOne({ | ||
where: { | ||
login: params.login | ||
}, | ||
raw: true | ||
}).then(user => { | ||
if (!user) | ||
throw new Error('Authentication failed. User not found.'); | ||
if (!bcrypt.compareSync(params.password || '', user.password)) | ||
throw new Error('Authentication failed. Wrong password.'); | ||
const payload = { | ||
login: user.login, | ||
id: user.id, | ||
time: new Date() | ||
}; | ||
|
||
var token = jwt.sign(payload, config.jwtSecret, { | ||
expiresIn: config.tokenExpireTime | ||
}); | ||
return token; | ||
}); | ||
} | ||
|
||
|
||
module.exports = { | ||
authenticate | ||
} |
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,11 @@ | ||
const sequelize = require('../db'); | ||
const Users = require('../models').User; | ||
|
||
const addUser = user => Users.create(user); | ||
|
||
const getUserByLogin = login => Users.findOne({where: {login}}); | ||
|
||
module.exports = { | ||
addUser, | ||
getUserByLogin | ||
} |
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,24 @@ | ||
const sequelize = require('../db'); | ||
const Users = require('../models').User; | ||
const Scores = require('../models').Score; | ||
|
||
const addUser = user => Users.create(user); | ||
const getUserByLogin = login => Users.findOne({where: {login}}); | ||
|
||
const getUsersWithScores = () => { | ||
return Users.findAll({ | ||
attributes: ['login', 'id'], | ||
include: [{ | ||
model: Scores, | ||
as: 'scores', | ||
attributes: ['level', 'question_correct', 'question_attempted'] | ||
}], | ||
}) | ||
.then(sequelize.getValues); | ||
} | ||
|
||
module.exports = { | ||
addUser, | ||
getUsersWithScores, | ||
getUserByLogin | ||
} |