@stanlemon/server-with-auth 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "@stanlemon/webdev/jest.config.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/server-with-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "A basic express web server setup with authentication baked in.",
|
|
5
5
|
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,11 +14,13 @@
|
|
|
14
14
|
"start": "NODE_ENV=development nodemon --ignore db.json ./app.js",
|
|
15
15
|
"lint": "eslint --ext js,jsx,ts,tsx ./",
|
|
16
16
|
"lint:format": "eslint --fix --ext js,jsx,ts,tsx ./",
|
|
17
|
-
"test": "
|
|
18
|
-
"test:
|
|
17
|
+
"test": "jest --detectOpenHandles",
|
|
18
|
+
"test:coverage": "jest --detectOpenHandles --coverage",
|
|
19
|
+
"test:watch": "jest --detectOpenHandles --watch"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"@stanlemon/server": "*",
|
|
23
|
+
"@stanlemon/webdev": "*",
|
|
22
24
|
"bcryptjs": "^2.4.3",
|
|
23
25
|
"jsonwebtoken": "^8.5.1",
|
|
24
26
|
"lowdb": "^3.0.0",
|
|
@@ -29,6 +31,7 @@
|
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"@stanlemon/eslint-config": "*",
|
|
34
|
+
"@types/supertest": "^2.0.12",
|
|
32
35
|
"nodemon": "^2.0.16",
|
|
33
36
|
"supertest": "^6.2.3"
|
|
34
37
|
}
|
package/src/createAppServer.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
} from "@stanlemon/server";
|
|
6
6
|
import passport from "passport";
|
|
7
7
|
import { Strategy as JwtStrategy, ExtractJwt } from "passport-jwt";
|
|
8
|
+
import shortid from "shortid";
|
|
8
9
|
import defaultUserSchema from "./schema/user.js";
|
|
9
10
|
import checkAuth from "./checkAuth.js";
|
|
10
11
|
import auth from "./routes/auth.js";
|
|
@@ -39,13 +40,17 @@ export default function createAppServer(options) {
|
|
|
39
40
|
updateUser,
|
|
40
41
|
} = { ...DEFAULTS, ...options };
|
|
41
42
|
|
|
43
|
+
const app = createBaseAppServer({ port, webpack, start });
|
|
44
|
+
|
|
45
|
+
if (process.env.NODE_ENV === "test") {
|
|
46
|
+
return app;
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
if (!process.env.JWT_SECRET) {
|
|
43
50
|
console.warn("You need to specify a secret.");
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
const secret = process.env.JWT_SECRET ||
|
|
47
|
-
|
|
48
|
-
const app = createBaseAppServer({ port, webpack, start });
|
|
53
|
+
const secret = process.env.JWT_SECRET || shortid.generate();
|
|
49
54
|
|
|
50
55
|
passport.use(
|
|
51
56
|
"jwt",
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { Low, JSONFile } from "lowdb";
|
|
1
|
+
import { Low, JSONFile, Memory } from "lowdb";
|
|
2
2
|
import { v4 as uuidv4 } from "uuid";
|
|
3
3
|
import shortid from "shortid";
|
|
4
4
|
import bcrypt from "bcryptjs";
|
|
5
5
|
|
|
6
|
+
const DEFAULT_ADAPTER =
|
|
7
|
+
process.env.NODE_ENV === "test" ? new Memory() : new JSONFile("./db.json");
|
|
8
|
+
|
|
6
9
|
export default class SimpleUsersDao {
|
|
7
|
-
constructor(seeds = [], adapter =
|
|
10
|
+
constructor(seeds = [], adapter = DEFAULT_ADAPTER) {
|
|
8
11
|
this.db = new Low(adapter);
|
|
9
12
|
|
|
10
13
|
this.db.read().then(() => {
|
|
@@ -16,6 +19,18 @@ export default class SimpleUsersDao {
|
|
|
16
19
|
});
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
getDb() {
|
|
23
|
+
return this.db;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
generateId() {
|
|
27
|
+
return uuidv4();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
generateVerificationToken() {
|
|
31
|
+
return shortid.generate();
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
getUserById = (userId) => {
|
|
20
35
|
return this.db.data.users
|
|
21
36
|
.filter((user) => {
|
|
@@ -66,8 +81,8 @@ export default class SimpleUsersDao {
|
|
|
66
81
|
const data = {
|
|
67
82
|
...user,
|
|
68
83
|
password: bcrypt.hashSync(user.password, 10),
|
|
69
|
-
id:
|
|
70
|
-
verification_token:
|
|
84
|
+
id: this.generateId(),
|
|
85
|
+
verification_token: this.generateVerificationToken(),
|
|
71
86
|
created_at: now,
|
|
72
87
|
last_updated: now,
|
|
73
88
|
};
|
package/src/routes/auth.test.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
1
4
|
import request from "supertest";
|
|
2
5
|
import { Memory } from "lowdb";
|
|
3
6
|
import createAppServer from "../createAppServer";
|
|
@@ -8,7 +11,10 @@ process.env.JWT_SECRET = "SECRET";
|
|
|
8
11
|
|
|
9
12
|
let users = new SimpleUsersDao([], new Memory());
|
|
10
13
|
|
|
14
|
+
// We want to explicitly test functionality we disable during testing
|
|
15
|
+
process.env.NODE_ENV = "override";
|
|
11
16
|
const app = createAppServer({ ...users, start: false });
|
|
17
|
+
process.env.NODE_ENV = "test";
|
|
12
18
|
|
|
13
19
|
describe("/auth", () => {
|
|
14
20
|
let userId;
|