Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 7f11b6c

Browse files
author
Luciano Nooijen
committed
Fixed failing tests and prepared for release
1 parent 3c84e05 commit 7f11b6c

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const debug = true || false;
5050
const blog = nodeBlog(client, host, user, database, password, debug);
5151
```
5252

53+
For authentication you should set the following environment (`process.env.[variable] = value`) variables, or the auth methods will not work:
54+
```
55+
SALT_ROUNDS=number
56+
JWT_SECRET=string
57+
JWT_EXPIRES_IN_DAYS=number_of_days
58+
```
59+
5360
Then you can use the imported functions as you wish, for example:
5461

5562
```js
@@ -67,8 +74,10 @@ authors.add(blog, authorObject)
6774
authors.modify(blog, id, modifiedData)
6875
authors.delete(blog, id)
6976

70-
auth.authenticate(blog, username, password) // Returns JWT
71-
auth.validate(blog, username, password) // Valid if data object is returned
77+
auth.authenticate(blog, username, password) // Returns true/false
78+
auth.generateToken(blog, username, password) // Returns JWT, throws error if invalid credentials
79+
auth.decode(jwt) // Returns decoded object
80+
auth.validate(blog, username, password) // Returns true/false
7281

7382
users.list(blog)
7483
users.get(blog, id)

controllers/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const generateToken = async (knex, username, password) => {
4040
};
4141

4242
const validateToken = async (knex, token) => {
43-
let decodedToken = '';
43+
let decodedToken = null;
4444

4545
// Check if token can be decoded, is valid format
4646
try {

src/create-node-blog.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/* eslint-disable object-curly-newline */ // TODO: Write docs!!!!!!
1+
/* eslint-disable object-curly-newline */
22

33
const getKnexInstance = require('knex');
44
const generateKnexfile = require('../database/generate-knexfile');
5+
const { authHelper } = require('../helpers');
56

67
const {
78
Authors,
@@ -20,7 +21,9 @@ const authors = {
2021
};
2122

2223
const auth = {
24+
generateToken: Auth.generateToken,
2325
authenticate: Auth.authenticateUser,
26+
decode: authHelper.decodeJWT,
2427
validate: Auth.validateToken,
2528
};
2629

tests/controllers/auth.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('Auth Controller', () => {
111111
expect.assertions(1);
112112
const { username } = testUser;
113113
const tokenPayloadData = await generateTokenPayload(blog, username);
114-
const validToken = authHelper.generateJWT(blog, tokenPayloadData);
114+
const validToken = authHelper.generateJWT(tokenPayloadData);
115115
const validTokenIsValid = await validateToken(blog, validToken);
116116
expect(validTokenIsValid).toBe(true);
117117
});

tests/controllers/users.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Test if Users CRUD operations are working correctly', () => {
7070
const addedUser = await addUser(blog, newUser);
7171
const hashed = addedUser.password;
7272
const isValidHash =
73-
await checkPasswordHash(blog, unhashedPassword, hashed);
73+
await checkPasswordHash(unhashedPassword, hashed);
7474
expect(hashed).not.toBe(newUser.password);
7575
expect(isValidHash).toBe(true);
7676
});

0 commit comments

Comments
 (0)