auth-common 0.0.0-stage → 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/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/middlewares/UserDetails.d.ts +16 -0
- package/build/middlewares/UserDetails.js +12 -0
- package/build/middlewares/ValidateUser.d.ts +2 -0
- package/build/middlewares/ValidateUser.js +8 -0
- package/package.json +27 -4
- package/src/index.ts +2 -0
- package/src/middlewares/UserDetails.ts +38 -0
- package/src/middlewares/ValidateUser.ts +14 -0
- package/tsconfig.json +15 -0
- package/README.md +0 -3
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Express {
|
|
4
|
+
interface Request {
|
|
5
|
+
session?: {
|
|
6
|
+
token?: string;
|
|
7
|
+
};
|
|
8
|
+
currentUser?: {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
email: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export declare const userDetails: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import jwt from "jsonwebtoken";
|
|
2
|
+
export const userDetails = async (req, res, next) => {
|
|
3
|
+
try {
|
|
4
|
+
const token = req?.session?.token;
|
|
5
|
+
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
|
|
6
|
+
req.currentUser = payload;
|
|
7
|
+
next();
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
next(error);
|
|
11
|
+
}
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auth-common",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"clean": "del-cli ./build/*",
|
|
8
|
+
"build": "npm run clean && tsc",
|
|
9
|
+
"pub": "git add . && git commit -m \"Updates\" && git push origin main && npm run build && npm version patch && npm publish "
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"type": "commonjs",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"del-cli": "^8.0.1",
|
|
17
|
+
"typescript": "^7.0.2"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/cookie-session": "^2.0.49",
|
|
21
|
+
"@types/express": "^5.0.6",
|
|
22
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
23
|
+
"cookie-session": "^2.1.1",
|
|
24
|
+
"error-common": "^1.0.3",
|
|
25
|
+
"express": "^5.3.0",
|
|
26
|
+
"express-validator": "^7.3.2",
|
|
27
|
+
"jsonwebtoken": "^9.0.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
namespace Express {
|
|
6
|
+
interface Request {
|
|
7
|
+
session?: {
|
|
8
|
+
token?: string;
|
|
9
|
+
};
|
|
10
|
+
currentUser?: {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface JwtPayload {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
email: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const userDetails = async (
|
|
26
|
+
req: Request,
|
|
27
|
+
res: Response,
|
|
28
|
+
next: NextFunction,
|
|
29
|
+
) => {
|
|
30
|
+
try {
|
|
31
|
+
const token = req?.session?.token;
|
|
32
|
+
const payload = jwt.verify(token!, process.env.TOKEN_SECRET!);
|
|
33
|
+
req.currentUser = payload as JwtPayload;
|
|
34
|
+
next();
|
|
35
|
+
} catch (error) {
|
|
36
|
+
next(error);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from "express";
|
|
2
|
+
import { UnauthorizeError } from "error-common";
|
|
3
|
+
|
|
4
|
+
export const validateUser = (
|
|
5
|
+
req: Request,
|
|
6
|
+
res: Response,
|
|
7
|
+
next: NextFunction,
|
|
8
|
+
) => {
|
|
9
|
+
const token = req.session?.token;
|
|
10
|
+
if (!token) {
|
|
11
|
+
throw new UnauthorizeError();
|
|
12
|
+
}
|
|
13
|
+
next();
|
|
14
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
// "module": "nodenext",
|
|
7
|
+
"target": "es2022",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"emitDeclarationOnly": false
|
|
13
|
+
},
|
|
14
|
+
"include": ["src"]
|
|
15
|
+
}
|
package/README.md
DELETED