@stanlemon/server-with-auth 0.1.8 → 0.1.11
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 +2 -2
- package/src/routes/auth.js +21 -25
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.11",
|
|
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",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@stanlemon/eslint-config": "*",
|
|
34
34
|
"@types/supertest": "^2.0.12",
|
|
35
|
-
"nodemon": "^2.0.
|
|
35
|
+
"nodemon": "^2.0.18",
|
|
36
36
|
"supertest": "^6.2.3"
|
|
37
37
|
}
|
|
38
38
|
}
|
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,33 +21,29 @@ export default function authRoutes({
|
|
|
21
21
|
}) {
|
|
22
22
|
const router = Router();
|
|
23
23
|
|
|
24
|
-
router.get(
|
|
25
|
-
|
|
26
|
-
passport.authenticate("jwt", { session: false }),
|
|
27
|
-
async (req, res) => {
|
|
28
|
-
const userId = req.user;
|
|
29
|
-
|
|
30
|
-
if (!userId) {
|
|
31
|
-
res.status(401).json({
|
|
32
|
-
token: false,
|
|
33
|
-
user: false,
|
|
34
|
-
});
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
24
|
+
router.get("/auth/session", checkAuth(), async (req, res) => {
|
|
25
|
+
const userId = req.user;
|
|
37
26
|
|
|
38
|
-
|
|
27
|
+
if (!userId) {
|
|
28
|
+
res.status(401).json({
|
|
29
|
+
token: false,
|
|
30
|
+
user: false,
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
39
34
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
const user = await getUserById(userId);
|
|
36
|
+
|
|
37
|
+
if (!user) {
|
|
38
|
+
res.status(401).json({
|
|
39
|
+
token: false,
|
|
40
|
+
user: false,
|
|
41
|
+
});
|
|
42
|
+
} else {
|
|
43
|
+
const token = jwt.sign(user.id, secret);
|
|
44
|
+
res.status(200).json({ token, user: formatOutput(user, ["password"]) });
|
|
49
45
|
}
|
|
50
|
-
);
|
|
46
|
+
});
|
|
51
47
|
|
|
52
48
|
router.post("/auth/login", async (req, res) => {
|
|
53
49
|
const user = await getUserByUsernameAndPassword(
|