@naman_deep_singh/security 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/README.md +42 -0
- package/dist/index.js +21 -11
- package/package.json +3 -2
- package/tsconfig.json +13 -7
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @naman_deep_singh/security
|
|
2
|
+
|
|
3
|
+
Security utilities for password hashing and JWT token management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @naman_deep_singh/security
|
|
9
|
+
or
|
|
10
|
+
pnpm add @naman_deep_singh/security
|
|
11
|
+
or
|
|
12
|
+
yarn add @naman_deep_singh/security
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { hashPassword, verifyPassword, generateToken, verifyToken } from '@naman_deep_singh/security';
|
|
19
|
+
|
|
20
|
+
// Password hashing
|
|
21
|
+
const hashedPassword = await hashPassword('mypassword');
|
|
22
|
+
const isValid = await verifyPassword('mypassword', hashedPassword);
|
|
23
|
+
|
|
24
|
+
// JWT tokens
|
|
25
|
+
const token = generateToken({ userId: 1 }, 'your-secret-key');
|
|
26
|
+
const decoded = verifyToken(token, 'your-secret-key');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API
|
|
30
|
+
|
|
31
|
+
### Password Functions
|
|
32
|
+
- `hashPassword(password: string): Promise<string>` - Hash a password using bcrypt
|
|
33
|
+
- `verifyPassword(password: string, hash: string): Promise<boolean>` - Verify password against hash
|
|
34
|
+
|
|
35
|
+
### JWT Functions
|
|
36
|
+
- `generateToken(payload: object, secret: string, expiresIn?: string): string` - Generate JWT token
|
|
37
|
+
- `verifyToken(token: string, secret: string): any` - Verify and decode JWT token
|
|
38
|
+
|
|
39
|
+
## Dependencies
|
|
40
|
+
|
|
41
|
+
- bcryptjs - For password hashing
|
|
42
|
+
- jsonwebtoken - For JWT token management
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.verifyToken = exports.signToken = exports.comparePassword = exports.hashPassword = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
3
9
|
// 🧱 Password helpers
|
|
4
|
-
|
|
5
|
-
const salt = await
|
|
6
|
-
return
|
|
10
|
+
const hashPassword = async (password) => {
|
|
11
|
+
const salt = await bcryptjs_1.default.genSalt(10);
|
|
12
|
+
return bcryptjs_1.default.hash(password, salt);
|
|
7
13
|
};
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
exports.hashPassword = hashPassword;
|
|
15
|
+
const comparePassword = async (password, hash) => {
|
|
16
|
+
return bcryptjs_1.default.compare(password, hash);
|
|
10
17
|
};
|
|
18
|
+
exports.comparePassword = comparePassword;
|
|
11
19
|
// 🧩 JWT helpers
|
|
12
|
-
|
|
13
|
-
return
|
|
20
|
+
const signToken = (payload, secret, expiresIn = "1h") => {
|
|
21
|
+
return jsonwebtoken_1.default.sign(payload, secret, { expiresIn, algorithm: "HS256" });
|
|
14
22
|
};
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
exports.signToken = signToken;
|
|
24
|
+
const verifyToken = (token, secret) => {
|
|
25
|
+
return jsonwebtoken_1.default.verify(token, secret);
|
|
17
26
|
};
|
|
27
|
+
exports.verifyToken = verifyToken;
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naman_deep_singh/security",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"scripts": {
|
|
@@ -19,4 +20,4 @@
|
|
|
19
20
|
"@types/jsonwebtoken": "^9.0.10",
|
|
20
21
|
"typescript": "^5.9.3"
|
|
21
22
|
}
|
|
22
|
-
}
|
|
23
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2020",
|
|
4
|
-
"module": "
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "dist",
|
|
7
|
-
"rootDir": "src",
|
|
4
|
+
"module": "CommonJS",
|
|
8
5
|
"moduleResolution": "node",
|
|
9
|
-
"
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "dist",
|
|
10
8
|
"strict": true,
|
|
11
|
-
"
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"baseUrl": ".",
|
|
14
|
+
"paths": {
|
|
15
|
+
"*": ["*", "*.ts", "*.js"]
|
|
16
|
+
}
|
|
12
17
|
},
|
|
13
|
-
"include": ["src"]
|
|
18
|
+
"include": ["src"],
|
|
19
|
+
"exclude": ["node_modules"]
|
|
14
20
|
}
|