Skip to content

Commit

Permalink
Merge bc468e9 into bdee3c0
Browse files Browse the repository at this point in the history
  • Loading branch information
olusoladavid committed Aug 14, 2018
2 parents bdee3c0 + bc468e9 commit 2493b39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
11 changes: 5 additions & 6 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { validationResult } from 'express-validator/check';
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 +53,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 +99,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 2493b39

Please sign in to comment.