Skip to content

Commit

Permalink
bug(Auth): set expiry on token for auth/login endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
olusoladavid committed Aug 13, 2018
1 parent 72dc8e5 commit d9ec6ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 5 additions & 5 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { query } from '../db/index';
import queries from '../db/queries';
import signAuthToken from '../utils/signAuthToken';


class userController {
Expand Down Expand Up @@ -53,9 +54,8 @@ class userController {

// create token using new data and sign with password hash+lastLogin+lastLogout
const userInfo = newUser.rows[0];
const jwtSecret = process.env.SECRET_KEY;
const data = { email: userInfo.email, createdOn: userInfo.created_on };
const token = jwt.sign(data, jwtSecret, { expiresIn: '2h' });
const token = signAuthToken(data);

// signed token - 201
res.status(201).json({ token });
Expand Down Expand Up @@ -100,10 +100,10 @@ class userController {
return;
}
// create token
const jwtSecret = process.env.SECRET_KEY;
const token = jwt.sign({
const data = {
email: userData.rows[0].email, createdOn: userData.rows[0].created_on,
}, jwtSecret);
};
const token = signAuthToken(data);
// return signed token - 200
res.status(200).json({ token });
});
Expand Down
12 changes: 12 additions & 0 deletions server/utils/signAuthToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';

dotenv.config();

const signAuthToken = (data) => {
const jwtSecret = process.env.SECRET_KEY;
const options = { expiresIn: '2h' };
return jwt.sign(data, jwtSecret, options);
};

export default signAuthToken;
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('/GET entries', () => {
chai
.request(app)
.get('/api/v1/entries')
.set('Authorization', null)
.set('Authorization', '')
.end((err, res) => {
expect(res).to.have.status(401);
expect(res.body).to.be.an('object');
Expand Down

0 comments on commit d9ec6ea

Please sign in to comment.