@skillstew/common 1.0.18 → 1.0.20
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.
|
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AuthMiddleware = void 0;
|
|
4
4
|
const UnauthenticatedError_1 = require("../errors/UnauthenticatedError");
|
|
5
5
|
const JwtHelper_1 = require("../jwt-utils/JwtHelper");
|
|
6
|
+
const JwtErrors_1 = require("../errors/JwtErrors");
|
|
7
|
+
const HttpStatus_1 = require("../constants/HttpStatus");
|
|
6
8
|
class AuthMiddleware {
|
|
7
9
|
constructor(userAccessTokenSecret, expertAccessTokenSecret, adminAccessTokenSecret) {
|
|
8
|
-
this.verify = (req,
|
|
10
|
+
this.verify = (req, res, next) => {
|
|
9
11
|
var _a;
|
|
10
12
|
try {
|
|
11
13
|
const token = (_a = req.headers["authorization"]) === null || _a === void 0 ? void 0 : _a.split(" ")[1];
|
|
@@ -19,6 +21,24 @@ class AuthMiddleware {
|
|
|
19
21
|
next();
|
|
20
22
|
}
|
|
21
23
|
catch (err) {
|
|
24
|
+
if (err instanceof JwtErrors_1.AccessTokenVerifyError) {
|
|
25
|
+
res
|
|
26
|
+
.status(HttpStatus_1.HttpStatus.UNAUTHORIZED)
|
|
27
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
else if (err instanceof JwtErrors_1.InvalidTokenError) {
|
|
31
|
+
res
|
|
32
|
+
.status(HttpStatus_1.HttpStatus.UNAUTHORIZED)
|
|
33
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
else if (err instanceof UnauthenticatedError_1.UnauthenticatedError) {
|
|
37
|
+
res
|
|
38
|
+
.status(HttpStatus_1.HttpStatus.UNAUTHORIZED)
|
|
39
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
22
42
|
next(err);
|
|
23
43
|
}
|
|
24
44
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { RequestHandler } from "express";
|
|
2
2
|
import { UnauthenticatedError } from "../errors/UnauthenticatedError";
|
|
3
3
|
import { JwtHelper } from "../jwt-utils/JwtHelper";
|
|
4
|
+
import { AccessTokenVerifyError, InvalidTokenError } from "../errors/JwtErrors";
|
|
5
|
+
import { HttpStatus } from "../constants/HttpStatus";
|
|
4
6
|
|
|
5
7
|
export class AuthMiddleware {
|
|
6
8
|
private _jwtHelper: JwtHelper;
|
|
@@ -17,12 +19,13 @@ export class AuthMiddleware {
|
|
|
17
19
|
});
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
verify: RequestHandler = (req,
|
|
22
|
+
verify: RequestHandler = (req, res, next) => {
|
|
21
23
|
try {
|
|
22
24
|
const token = req.headers["authorization"]?.split(" ")[1];
|
|
23
25
|
if (!token) {
|
|
24
26
|
throw new UnauthenticatedError();
|
|
25
27
|
}
|
|
28
|
+
|
|
26
29
|
const payload = this._jwtHelper.verifyAccessToken(token);
|
|
27
30
|
req.user = {
|
|
28
31
|
id: payload.userId,
|
|
@@ -33,6 +36,22 @@ export class AuthMiddleware {
|
|
|
33
36
|
};
|
|
34
37
|
next();
|
|
35
38
|
} catch (err) {
|
|
39
|
+
if (err instanceof AccessTokenVerifyError) {
|
|
40
|
+
res
|
|
41
|
+
.status(HttpStatus.UNAUTHORIZED)
|
|
42
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
43
|
+
return;
|
|
44
|
+
} else if (err instanceof InvalidTokenError) {
|
|
45
|
+
res
|
|
46
|
+
.status(HttpStatus.UNAUTHORIZED)
|
|
47
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
48
|
+
return;
|
|
49
|
+
} else if (err instanceof UnauthenticatedError) {
|
|
50
|
+
res
|
|
51
|
+
.status(HttpStatus.UNAUTHORIZED)
|
|
52
|
+
.json({ success: false, message: err.message, code: err.code });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
36
55
|
next(err);
|
|
37
56
|
}
|
|
38
57
|
};
|