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.
@@ -0,0 +1,2 @@
1
+ export * from "./middlewares/UserDetails";
2
+ export * from "./middlewares/ValidateUser";
package/build/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./middlewares/UserDetails";
2
+ export * from "./middlewares/ValidateUser";
@@ -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
+ };
@@ -0,0 +1,2 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ export declare const validateUser: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,8 @@
1
+ import { UnauthorizeError } from "error-common";
2
+ export const validateUser = (req, res, next) => {
3
+ const token = req.session?.token;
4
+ if (!token) {
5
+ throw new UnauthorizeError();
6
+ }
7
+ next();
8
+ };
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "auth-common",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
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,2 @@
1
+ export * from "./middlewares/UserDetails";
2
+ export * from "./middlewares/ValidateUser";
@@ -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
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.