@occupop/lib-auth 1.0.0 → 1.0.2
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/dist/index.d.ts +7 -6
- package/dist/index.js +14 -21
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type { IncomingMessage
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
2
|
import { type AwilixContainer } from '@occupop/lib-container';
|
|
3
3
|
type Request = IncomingMessage & {
|
|
4
4
|
container?: AwilixContainer;
|
|
5
5
|
};
|
|
6
|
-
type Response = OutgoingMessage;
|
|
7
6
|
export type AuthUser = {
|
|
8
7
|
readonly userUuid: string;
|
|
9
8
|
readonly hiringCompanyUuid?: string;
|
|
@@ -22,10 +21,12 @@ export declare function getAuthUser(req: Request): AuthUser | null;
|
|
|
22
21
|
export declare function getScopedUser(req: Request): ScopedUser | null;
|
|
23
22
|
export declare function makeAuthContextValueFunction<IContainer extends AwilixContainer>({ container }: {
|
|
24
23
|
container: IContainer;
|
|
25
|
-
}): (req
|
|
24
|
+
}): ({ req }: {
|
|
25
|
+
req: Request;
|
|
26
|
+
}) => Promise<{
|
|
26
27
|
container: AwilixContainer<any>;
|
|
27
|
-
}
|
|
28
|
-
export declare function
|
|
28
|
+
}>;
|
|
29
|
+
export declare function makeAuthContextMiddleware<IContainer extends AwilixContainer>({ container, }: {
|
|
29
30
|
container: IContainer;
|
|
30
|
-
}): (req: Request
|
|
31
|
+
}): (req: Request) => Promise<void>;
|
|
31
32
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.getAuthUser = getAuthUser;
|
|
4
|
-
exports.getScopedUser = getScopedUser;
|
|
5
|
-
exports.makeAuthContextValueFunction = makeAuthContextValueFunction;
|
|
6
|
-
exports.makeAuthContextMiddlewareFunction = makeAuthContextMiddlewareFunction;
|
|
7
|
-
const lib_container_1 = require("@occupop/lib-container");
|
|
8
|
-
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
1
|
+
import { asValue } from '@occupop/lib-container';
|
|
2
|
+
import { verify } from 'jsonwebtoken';
|
|
9
3
|
function getToken(req) {
|
|
10
|
-
|
|
11
|
-
return (_a = req.headers.authorization) === null || _a === void 0 ? void 0 : _a.replace(/^Bearer /, '');
|
|
4
|
+
return req.headers.authorization?.replace(/^Bearer /, '');
|
|
12
5
|
}
|
|
13
6
|
function checkToken(token) {
|
|
14
7
|
if (!token)
|
|
@@ -16,7 +9,7 @@ function checkToken(token) {
|
|
|
16
9
|
if (!process.env.JWT_SECRET)
|
|
17
10
|
throw 'Env var JWT_SECRET not set?';
|
|
18
11
|
try {
|
|
19
|
-
return
|
|
12
|
+
return verify(token, process.env.JWT_SECRET);
|
|
20
13
|
}
|
|
21
14
|
catch (e) {
|
|
22
15
|
return null;
|
|
@@ -26,34 +19,34 @@ function getUserPayload(req) {
|
|
|
26
19
|
const token = getToken(req);
|
|
27
20
|
return checkToken(token);
|
|
28
21
|
}
|
|
29
|
-
function getAuthUser(req) {
|
|
22
|
+
export function getAuthUser(req) {
|
|
30
23
|
const user = getUserPayload(req);
|
|
31
|
-
if (user
|
|
24
|
+
if (user?.userUuid)
|
|
32
25
|
return user;
|
|
33
26
|
return null;
|
|
34
27
|
}
|
|
35
|
-
function getScopedUser(req) {
|
|
28
|
+
export function getScopedUser(req) {
|
|
36
29
|
const user = getUserPayload(req);
|
|
37
30
|
if (user && user.userUuid === undefined)
|
|
38
31
|
return user;
|
|
39
32
|
return null;
|
|
40
33
|
}
|
|
41
|
-
function makeAuthContextValueFunction({ container }) {
|
|
42
|
-
return (req
|
|
34
|
+
export function makeAuthContextValueFunction({ container }) {
|
|
35
|
+
return async ({ req }) => {
|
|
43
36
|
const authUser = getAuthUser(req);
|
|
44
37
|
const scopedUser = getScopedUser(req);
|
|
45
38
|
const scopedContainer = container.createScope();
|
|
46
39
|
if (authUser)
|
|
47
|
-
container.register('authUser',
|
|
40
|
+
container.register('authUser', asValue(authUser));
|
|
48
41
|
if (scopedUser)
|
|
49
|
-
container.register('scopedUser',
|
|
42
|
+
container.register('scopedUser', asValue(scopedUser));
|
|
50
43
|
return { container: scopedContainer };
|
|
51
44
|
};
|
|
52
45
|
}
|
|
53
|
-
function
|
|
54
|
-
return (req
|
|
46
|
+
export function makeAuthContextMiddleware({ container, }) {
|
|
47
|
+
return async (req) => {
|
|
55
48
|
const authContextValueFunction = makeAuthContextValueFunction({ container });
|
|
56
|
-
const contextValue = authContextValueFunction(req
|
|
49
|
+
const contextValue = await authContextValueFunction({ req });
|
|
57
50
|
req.container = contextValue.container;
|
|
58
51
|
};
|
|
59
52
|
}
|