Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Token Refresh Endpoint #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions api/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const User = require('../models/User');
const authService = require('../services/auth.service');
const bcryptService = require('../services/bcrypt.service');
const cryptoService = require('../services/crypto.service');

const UserController = () => {
const register = async (req, res) => {
Expand All @@ -13,8 +14,7 @@ const UserController = () => {
password: body.password,
});
const token = authService().issue({ id: user.id });

return res.status(200).json({ token, user });
return res.status(200).json({ token, user, refreshToken: user.refreshToken });
} catch (err) {
console.log(err);
return res.status(500).json({ msg: 'Internal server error' });
Expand All @@ -25,35 +25,38 @@ const UserController = () => {
};

const login = async (req, res) => {
const { email, password } = req.body;

if (email && password) {
try {
const user = await User
.findOne({
where: {
email,
},
});

if (!user) {
return res.status(400).json({ msg: 'Bad Request: User not found' });
const { email, password, refreshToken } = req.body;
let user;

if (email) {
if (password || refreshToken) {
try {
user = await User
.findOne({
where: {
email,
},
});
if (!user) {
return res.status(400).json({ msg: 'Bad Request: User not found' });
}
} catch (err) {
console.log(err);
return res.status(500).json({ msg: 'Internal server error' });
}

if (bcryptService().comparePassword(password, user.password)) {
if ((password && bcryptService().comparePassword(password, user.password)) ||
(refreshToken && user.refreshToken === refreshToken)
) {
const token = authService().issue({ id: user.id });

return res.status(200).json({ token, user });
const newRefreshToken = cryptoService().generateRefreshToken();
user.refreshToken = newRefreshToken;
user.save();
return res.status(200).json({ token, user, refreshToken: newRefreshToken });
}

return res.status(401).json({ msg: 'Unauthorized' });
} catch (err) {
console.log(err);
return res.status(500).json({ msg: 'Internal server error' });
}
}

return res.status(400).json({ msg: 'Bad Request: Email or password is wrong' });
return res.status(401).json({ msg: 'Unauthorized' });
};

const validate = (req, res) => {
Expand Down
7 changes: 6 additions & 1 deletion api/models/User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Sequelize = require('sequelize');
const bcryptService = require('../services/bcrypt.service');

const cryptoService = require('../services/crypto.service');
const sequelize = require('../../config/database');

const hooks = {
Expand All @@ -19,13 +19,18 @@ const User = sequelize.define('User', {
password: {
type: Sequelize.STRING,
},
refreshToken: {
type: Sequelize.STRING,
defaultValue: () => cryptoService().generateRefreshToken(),
},
}, { hooks, tableName });

// eslint-disable-next-line
User.prototype.toJSON = function () {
const values = Object.assign({}, this.get());

delete values.password;
delete values.refreshToken;

return values;
};
Expand Down
11 changes: 11 additions & 0 deletions api/services/crypto.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const crypto = require('crypto');

const cryptoService = () => {
const generateRefreshToken = () => crypto.randomBytes(20).toString('hex');

return {
generateRefreshToken,
};
};

module.exports = cryptoService;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"crypto": "^1.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's now build in on nodejs, but the people using this is still a lot.

"express": "^4.16.3",
"express-routes-mapper": "^1.0.2",
"helmet": "^3.12.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,11 @@ crypto-random-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"

crypto@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==

[email protected], "cssom@>= 0.3.2 < 0.4.0":
version "0.3.2"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.2.tgz#b8036170c79f07a90ff2f16e22284027a243848b"
Expand Down