@stanlemon/server-with-auth 0.1.7 → 0.1.10
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/package.json
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LowSync, JSONFileSync, MemorySync } 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
6
|
const DEFAULT_ADAPTER =
|
|
7
|
-
process.env.NODE_ENV === "test"
|
|
7
|
+
process.env.NODE_ENV === "test"
|
|
8
|
+
? new MemorySync()
|
|
9
|
+
: new JSONFileSync("./db.json");
|
|
8
10
|
|
|
9
11
|
export default class SimpleUsersDao {
|
|
10
12
|
constructor(seeds = [], adapter = DEFAULT_ADAPTER) {
|
|
11
|
-
this.db = new
|
|
13
|
+
this.db = new LowSync(adapter);
|
|
12
14
|
|
|
13
|
-
this.db.read()
|
|
14
|
-
this.db.data ||= { users: [] };
|
|
15
|
+
this.db.read();
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
this.db.data ||= { users: [] };
|
|
18
|
+
|
|
19
|
+
if (seeds.length > 0) {
|
|
20
|
+
seeds.forEach((user) => this.createUser(user));
|
|
21
|
+
}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
getDb() {
|
|
@@ -70,7 +72,7 @@ export default class SimpleUsersDao {
|
|
|
70
72
|
.shift();
|
|
71
73
|
};
|
|
72
74
|
|
|
73
|
-
createUser =
|
|
75
|
+
createUser = (user) => {
|
|
74
76
|
const existing = this.getUserByUsername(user.username);
|
|
75
77
|
|
|
76
78
|
if (existing) {
|
|
@@ -87,11 +89,11 @@ export default class SimpleUsersDao {
|
|
|
87
89
|
last_updated: now,
|
|
88
90
|
};
|
|
89
91
|
this.db.data.users.push(data);
|
|
90
|
-
|
|
92
|
+
this.db.write();
|
|
91
93
|
return data;
|
|
92
94
|
};
|
|
93
95
|
|
|
94
|
-
updateUser =
|
|
96
|
+
updateUser = (userId, user) => {
|
|
95
97
|
const now = new Date();
|
|
96
98
|
this.db.data.users = this.db.data.users.map((u) => {
|
|
97
99
|
if (u.id === userId) {
|
|
@@ -109,7 +111,7 @@ export default class SimpleUsersDao {
|
|
|
109
111
|
}
|
|
110
112
|
return u;
|
|
111
113
|
});
|
|
112
|
-
|
|
114
|
+
this.db.write();
|
|
113
115
|
return this.getUserById(userId);
|
|
114
116
|
};
|
|
115
117
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @jest-environment node
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { MemorySync } from "lowdb";
|
|
5
5
|
import SimpleUsersDao from "./simple-users-dao.js";
|
|
6
6
|
|
|
7
7
|
describe("simple-users-dao", () => {
|
|
@@ -15,7 +15,7 @@ describe("simple-users-dao", () => {
|
|
|
15
15
|
|
|
16
16
|
beforeEach(() => {
|
|
17
17
|
// Before each test reset our users database
|
|
18
|
-
users = new SimpleUsersDao([], new
|
|
18
|
+
users = new SimpleUsersDao([], new MemorySync());
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it("creates a user", async () => {
|
package/src/routes/auth.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { isEmpty } from "lodash-es";
|
|
2
2
|
import { Router } from "express";
|
|
3
|
-
import passport from "passport";
|
|
4
3
|
import jwt from "jsonwebtoken";
|
|
5
4
|
import {
|
|
6
5
|
schemaHandler,
|
|
7
6
|
formatOutput,
|
|
8
7
|
BadRequestException,
|
|
9
8
|
} from "@stanlemon/server";
|
|
9
|
+
import checkAuth from "../checkAuth.js";
|
|
10
10
|
|
|
11
11
|
/* eslint-disable max-lines-per-function */
|
|
12
12
|
export default function authRoutes({
|
|
@@ -21,39 +21,28 @@ export default function authRoutes({
|
|
|
21
21
|
}) {
|
|
22
22
|
const router = Router();
|
|
23
23
|
|
|
24
|
-
router.get("/auth/session", (req, res
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
router.get("/auth/session", checkAuth(), async (req, res) => {
|
|
25
|
+
const userId = req.user;
|
|
26
|
+
|
|
27
|
+
if (!userId) {
|
|
28
|
+
res.status(401).json({
|
|
29
|
+
token: false,
|
|
30
|
+
user: false,
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const user = await getUserById(userId);
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const user = await getUserById(userId);
|
|
43
|
-
|
|
44
|
-
if (!user) {
|
|
45
|
-
return res.status(401).json({
|
|
46
|
-
token: false,
|
|
47
|
-
user: false,
|
|
48
|
-
});
|
|
49
|
-
} else {
|
|
50
|
-
const token = jwt.sign(user.id, secret);
|
|
51
|
-
res
|
|
52
|
-
.status(200)
|
|
53
|
-
.json({ token, user: formatOutput(user, ["password"]) });
|
|
54
|
-
}
|
|
37
|
+
if (!user) {
|
|
38
|
+
res.status(401).json({
|
|
39
|
+
token: false,
|
|
40
|
+
user: false,
|
|
55
41
|
});
|
|
56
|
-
}
|
|
42
|
+
} else {
|
|
43
|
+
const token = jwt.sign(user.id, secret);
|
|
44
|
+
res.status(200).json({ token, user: formatOutput(user, ["password"]) });
|
|
45
|
+
}
|
|
57
46
|
});
|
|
58
47
|
|
|
59
48
|
router.post("/auth/login", async (req, res) => {
|
package/src/routes/auth.test.js
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* @jest-environment node
|
|
3
3
|
*/
|
|
4
4
|
import request from "supertest";
|
|
5
|
-
import {
|
|
5
|
+
import { MemorySync } from "lowdb";
|
|
6
6
|
import createAppServer from "../createAppServer";
|
|
7
7
|
import SimpleUsersDao from "../data/simple-users-dao.js";
|
|
8
8
|
|
|
9
9
|
// This suppresses a warning we don't need in tests
|
|
10
10
|
process.env.JWT_SECRET = "SECRET";
|
|
11
11
|
|
|
12
|
-
let users = new SimpleUsersDao([], new
|
|
12
|
+
let users = new SimpleUsersDao([], new MemorySync());
|
|
13
13
|
|
|
14
14
|
// We want to explicitly test functionality we disable during testing
|
|
15
15
|
process.env.NODE_ENV = "override";
|