@mattygt86/daturetech-auth-library 0.0.0-alpha
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/.eslintrc.json +34 -0
- package/LICENSE +21 -0
- package/README.md +132 -0
- package/dist/auth.abstract.d.ts +6 -0
- package/dist/auth.abstract.d.ts.map +1 -0
- package/dist/auth.abstract.js +6 -0
- package/dist/auth.d.ts +11 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.factory.d.ts +9 -0
- package/dist/auth.factory.d.ts.map +1 -0
- package/dist/auth.factory.js +18 -0
- package/dist/auth.js +24 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/providers/auth0.provider.d.ts +11 -0
- package/dist/providers/auth0.provider.d.ts.map +1 -0
- package/dist/providers/auth0.provider.js +61 -0
- package/package.json +35 -0
- package/src/auth.abstract.ts +5 -0
- package/src/auth.factory.ts +16 -0
- package/src/auth.ts +23 -0
- package/src/index.ts +4 -0
- package/src/providers/auth0.provider.ts +47 -0
- package/tsconfig.json +20 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es2021": true
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"eslint:recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended"
|
|
9
|
+
],
|
|
10
|
+
"parser": "@typescript-eslint/parser",
|
|
11
|
+
"parserOptions": {
|
|
12
|
+
"ecmaVersion": "latest",
|
|
13
|
+
"sourceType": "module"
|
|
14
|
+
},
|
|
15
|
+
"plugins": [
|
|
16
|
+
"@typescript-eslint"
|
|
17
|
+
],
|
|
18
|
+
"rules": {
|
|
19
|
+
"semi": [
|
|
20
|
+
"error",
|
|
21
|
+
"always"
|
|
22
|
+
],
|
|
23
|
+
"quotes": [
|
|
24
|
+
"error",
|
|
25
|
+
"double"
|
|
26
|
+
],
|
|
27
|
+
"@typescript-eslint/no-explicit-any": "warn",
|
|
28
|
+
"indent": [
|
|
29
|
+
"error",
|
|
30
|
+
2
|
|
31
|
+
],
|
|
32
|
+
"no-unused-vars": "warn"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MattyGT
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
```md
|
|
2
|
+
# DatureTech Authentication Library
|
|
3
|
+
|
|
4
|
+
The **DatureTech Authentication Library** provides an easy-to-use authentication solution for Express.js applications. It integrates with **Auth0** and provides middleware for handling authentication and authorization.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## **Features**
|
|
9
|
+
- 🚀 Simple middleware integration for authentication.
|
|
10
|
+
- 🔐 Secure token verification using **Auth0**.
|
|
11
|
+
- ✅ Role-based authorization support.
|
|
12
|
+
- 📦 Lightweight and easy to install.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## **Installation**
|
|
17
|
+
You can install the library via **npm**:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install daturetech-auth-library
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
If using a local package reference:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install file:../daturetech-auth-library/dist
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## **Usage**
|
|
32
|
+
|
|
33
|
+
### **1. Import the Library**
|
|
34
|
+
In your Express.js application, import and initialize the authentication middleware:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import express from "express";
|
|
38
|
+
import { AuthMiddleware } from "daturetech-auth-library";
|
|
39
|
+
|
|
40
|
+
const app = express();
|
|
41
|
+
|
|
42
|
+
// Initialize authentication middleware
|
|
43
|
+
const authMiddleware = new AuthMiddleware({
|
|
44
|
+
domain: process.env.AUTH0_DOMAIN,
|
|
45
|
+
audience: process.env.AUTH0_AUDIENCE,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
app.use(authMiddleware.protect()); // Apply authentication globally
|
|
49
|
+
|
|
50
|
+
app.get("/protected-route", authMiddleware.protect(), (req, res) => {
|
|
51
|
+
res.json({ message: "You have accessed a protected route!" });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
app.listen(5000, () => console.log("Server running on port 5000"));
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## **Configuration**
|
|
60
|
+
Ensure you have the required environment variables set in your `.env` file:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
AUTH0_DOMAIN=your-auth0-domain
|
|
64
|
+
AUTH0_AUDIENCE=your-auth0-audience
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## **API Reference**
|
|
70
|
+
|
|
71
|
+
### `authMiddleware.protect()`
|
|
72
|
+
Middleware function that protects routes by verifying the user's JWT.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
app.use(authMiddleware.protect());
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `authMiddleware.checkRole(role: string)`
|
|
79
|
+
Middleware function to restrict access based on user roles.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
app.get("/admin", authMiddleware.checkRole("admin"), (req, res) => {
|
|
83
|
+
res.json({ message: "Welcome, Admin!" });
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## **Development & Contribution**
|
|
90
|
+
1. Clone the repository:
|
|
91
|
+
```sh
|
|
92
|
+
git clone https://github.com/MattyGT/daturetech-auth-library.git
|
|
93
|
+
```
|
|
94
|
+
2. Install dependencies:
|
|
95
|
+
```sh
|
|
96
|
+
npm install
|
|
97
|
+
```
|
|
98
|
+
3. Build the library:
|
|
99
|
+
```sh
|
|
100
|
+
npm run build
|
|
101
|
+
```
|
|
102
|
+
4. Run tests:
|
|
103
|
+
```sh
|
|
104
|
+
npm test
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## **Publishing the Package**
|
|
110
|
+
To publish updates to **npm**, follow these steps:
|
|
111
|
+
|
|
112
|
+
1. Bump the version in `package.json`:
|
|
113
|
+
```sh
|
|
114
|
+
npm version patch
|
|
115
|
+
```
|
|
116
|
+
2. Publish the package:
|
|
117
|
+
```sh
|
|
118
|
+
npm publish --access public
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## **License**
|
|
124
|
+
This project is licensed under the MIT License.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## **Support**
|
|
129
|
+
For issues, please open a GitHub issue in the repository.
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
This provides a professional and comprehensive `README.md` that covers installation, usage, API references, and contribution guidelines. Let me know if you need any modifications! 🚀
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.abstract.d.ts","sourceRoot":"","sources":["../src/auth.abstract.ts"],"names":[],"mappings":"AAAA,8BAAsB,YAAY;IAC9B,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAC9D,QAAQ,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IACpD,QAAQ,CAAC,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CACnC"}
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class Auth {
|
|
2
|
+
private provider;
|
|
3
|
+
constructor(providerName: string, config: {
|
|
4
|
+
domain: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
});
|
|
7
|
+
login(email: string, password: string): Promise<void>;
|
|
8
|
+
resetPassword(email: string): Promise<void>;
|
|
9
|
+
logout(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAIA,qBAAa,IAAI;IACf,OAAO,CAAC,QAAQ,CAAe;gBAEnB,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;IAI9E,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAIrC,aAAa,CAAC,KAAK,EAAE,MAAM;IAI3B,MAAM;CAGP"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AuthProvider } from './auth.abstract';
|
|
2
|
+
declare class AuthFactory {
|
|
3
|
+
static createProvider(providerName: string, config: {
|
|
4
|
+
domain: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
}): AuthProvider;
|
|
7
|
+
}
|
|
8
|
+
export default AuthFactory;
|
|
9
|
+
//# sourceMappingURL=auth.factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.factory.d.ts","sourceRoot":"","sources":["../src/auth.factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAI/C,cAAM,WAAW;IACf,MAAM,CAAC,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,YAAY;CAQxG;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
const auth0_provider_1 = __importDefault(require("./providers/auth0.provider"));
|
|
7
|
+
// Factory to instantiate the correct provider
|
|
8
|
+
class AuthFactory {
|
|
9
|
+
static createProvider(providerName, config) {
|
|
10
|
+
switch (providerName.toLowerCase()) {
|
|
11
|
+
case 'auth0':
|
|
12
|
+
return new auth0_provider_1.default(config.domain, config.clientId);
|
|
13
|
+
default:
|
|
14
|
+
throw new Error(`Unsupported provider: ${providerName}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.default = AuthFactory;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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.Auth = void 0;
|
|
7
|
+
const auth_factory_1 = __importDefault(require("./auth.factory"));
|
|
8
|
+
// Main Auth class
|
|
9
|
+
class Auth {
|
|
10
|
+
constructor(providerName, config) {
|
|
11
|
+
this.provider = auth_factory_1.default.createProvider(providerName, config);
|
|
12
|
+
}
|
|
13
|
+
login(email, password) {
|
|
14
|
+
return this.provider.login(email, password);
|
|
15
|
+
}
|
|
16
|
+
resetPassword(email) {
|
|
17
|
+
return this.provider.resetPassword(email);
|
|
18
|
+
}
|
|
19
|
+
logout() {
|
|
20
|
+
return this.provider.logout();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Auth = Auth;
|
|
24
|
+
;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,eAAe,IAAI,CAAC;AACpB,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth0Provider = void 0;
|
|
4
|
+
const auth_1 = require("./auth");
|
|
5
|
+
exports.default = auth_1.Auth; // Adjust path based on your file structure
|
|
6
|
+
var auth0_provider_1 = require("./providers/auth0.provider"); // If you need direct provider access
|
|
7
|
+
Object.defineProperty(exports, "Auth0Provider", { enumerable: true, get: function () { return auth0_provider_1.Auth0Provider; } });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AuthProvider } from '../auth.abstract';
|
|
2
|
+
export declare class Auth0Provider extends AuthProvider {
|
|
3
|
+
private domain;
|
|
4
|
+
private clientId;
|
|
5
|
+
constructor(domain: string, clientId: string);
|
|
6
|
+
login(email: string, password: string): Promise<void>;
|
|
7
|
+
resetPassword(email: string): Promise<void>;
|
|
8
|
+
logout(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export default Auth0Provider;
|
|
11
|
+
//# sourceMappingURL=auth0.provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth0.provider.d.ts","sourceRoot":"","sources":["../../src/providers/auth0.provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD,qBAAa,aAAc,SAAQ,YAAY;IAC7C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAS;gBAEb,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAMtC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAerD,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY3C,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAI9B;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Auth0Provider = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const auth_abstract_1 = require("../auth.abstract");
|
|
18
|
+
class Auth0Provider extends auth_abstract_1.AuthProvider {
|
|
19
|
+
constructor(domain, clientId) {
|
|
20
|
+
super();
|
|
21
|
+
this.domain = domain;
|
|
22
|
+
this.clientId = clientId;
|
|
23
|
+
}
|
|
24
|
+
login(email, password) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
console.log(`Logging in with Auth0: ${email}`);
|
|
27
|
+
const response = yield axios_1.default.post(`https://${this.domain}/oauth/token`, {
|
|
28
|
+
client_id: this.clientId,
|
|
29
|
+
grant_type: 'password',
|
|
30
|
+
username: email,
|
|
31
|
+
password: password,
|
|
32
|
+
audience: `https://${this.domain}/userinfo`,
|
|
33
|
+
scope: 'openid profile email'
|
|
34
|
+
}, {
|
|
35
|
+
headers: { 'Content-Type': 'application/json' }
|
|
36
|
+
});
|
|
37
|
+
console.log('Auth0 Login Response:', response.data);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
resetPassword(email) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
console.log(`Resetting password with Auth0: ${email}`);
|
|
43
|
+
const response = yield axios_1.default.post(`https://${this.domain}/dbconnections/change_password`, {
|
|
44
|
+
client_id: this.clientId,
|
|
45
|
+
email: email,
|
|
46
|
+
connection: 'Username-Password-Authentication'
|
|
47
|
+
}, {
|
|
48
|
+
headers: { 'Content-Type': 'application/json' }
|
|
49
|
+
});
|
|
50
|
+
console.log('Auth0 Reset Password Response:', response.data);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
logout() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
console.log(`Logging out from Auth0`);
|
|
56
|
+
window.location.href = `https://${this.domain}/v2/logout?client_id=${this.clientId}&returnTo=${encodeURIComponent(window.location.origin)}`;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.Auth0Provider = Auth0Provider;
|
|
61
|
+
exports.default = Auth0Provider;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mattygt86/daturetech-auth-library",
|
|
3
|
+
"version": "0.0.0-alpha",
|
|
4
|
+
"description": "A TypeScript authentication library with provider abstraction",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"auth"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/MattyGT/daturetech_auth_library#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/MattyGT/daturetech_auth_library/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/MattyGT/daturetech_auth_library.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"author": "Matthew Tyler",
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"start": "node dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"axios": "^1.4.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^20.0.0",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^8.28.0",
|
|
31
|
+
"@typescript-eslint/parser": "^8.28.0",
|
|
32
|
+
"eslint": "^9.23.0",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AuthProvider } from './auth.abstract';
|
|
2
|
+
import Auth0Provider from './providers/auth0.provider';
|
|
3
|
+
|
|
4
|
+
// Factory to instantiate the correct provider
|
|
5
|
+
class AuthFactory {
|
|
6
|
+
static createProvider(providerName: string, config: { domain: string; clientId: string }): AuthProvider {
|
|
7
|
+
switch (providerName.toLowerCase()) {
|
|
8
|
+
case 'auth0':
|
|
9
|
+
return new Auth0Provider(config.domain, config.clientId);
|
|
10
|
+
default:
|
|
11
|
+
throw new Error(`Unsupported provider: ${providerName}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default AuthFactory;
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AuthProvider } from "./auth.abstract";
|
|
2
|
+
import AuthFactory from "./auth.factory";
|
|
3
|
+
|
|
4
|
+
// Main Auth class
|
|
5
|
+
export class Auth {
|
|
6
|
+
private provider: AuthProvider;
|
|
7
|
+
|
|
8
|
+
constructor(providerName: string, config: { domain: string; clientId: string }) {
|
|
9
|
+
this.provider = AuthFactory.createProvider(providerName, config);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
login(email: string, password: string) {
|
|
13
|
+
return this.provider.login(email, password);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
resetPassword(email: string) {
|
|
17
|
+
return this.provider.resetPassword(email);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
logout() {
|
|
21
|
+
return this.provider.logout();
|
|
22
|
+
}
|
|
23
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { AuthProvider } from '../auth.abstract';
|
|
3
|
+
|
|
4
|
+
export class Auth0Provider extends AuthProvider {
|
|
5
|
+
private domain: string;
|
|
6
|
+
private clientId: string;
|
|
7
|
+
|
|
8
|
+
constructor(domain: string, clientId: string) {
|
|
9
|
+
super();
|
|
10
|
+
this.domain = domain;
|
|
11
|
+
this.clientId = clientId;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async login(email: string, password: string): Promise<void> {
|
|
15
|
+
console.log(`Logging in with Auth0: ${email}`);
|
|
16
|
+
const response = await axios.post(`https://${this.domain}/oauth/token`, {
|
|
17
|
+
client_id: this.clientId,
|
|
18
|
+
grant_type: 'password',
|
|
19
|
+
username: email,
|
|
20
|
+
password: password,
|
|
21
|
+
audience: `https://${this.domain}/userinfo`,
|
|
22
|
+
scope: 'openid profile email'
|
|
23
|
+
}, {
|
|
24
|
+
headers: { 'Content-Type': 'application/json' }
|
|
25
|
+
});
|
|
26
|
+
console.log('Auth0 Login Response:', response.data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async resetPassword(email: string): Promise<void> {
|
|
30
|
+
console.log(`Resetting password with Auth0: ${email}`);
|
|
31
|
+
const response = await axios.post(`https://${this.domain}/dbconnections/change_password`, {
|
|
32
|
+
client_id: this.clientId,
|
|
33
|
+
email: email,
|
|
34
|
+
connection: 'Username-Password-Authentication'
|
|
35
|
+
}, {
|
|
36
|
+
headers: { 'Content-Type': 'application/json' }
|
|
37
|
+
});
|
|
38
|
+
console.log('Auth0 Reset Password Response:', response.data);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async logout(): Promise<void> {
|
|
42
|
+
console.log(`Logging out from Auth0`);
|
|
43
|
+
window.location.href = `https://${this.domain}/v2/logout?client_id=${this.clientId}&returnTo=${encodeURIComponent(window.location.origin)}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default Auth0Provider;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"strict": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"emitDeclarationOnly": false,
|
|
12
|
+
"moduleResolution": "node"
|
|
13
|
+
},
|
|
14
|
+
"include": [
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"exclude": [
|
|
18
|
+
"node_modules"
|
|
19
|
+
]
|
|
20
|
+
}
|