authx-sdk 1.0.0

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 ADDED
@@ -0,0 +1,56 @@
1
+ # AuthX Node.js SDK
2
+
3
+ Backend-to-backend SDK for AuthX APIs.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install authx-sdk
9
+ ```
10
+
11
+ ## Connection config
12
+
13
+ - `clientId` and `clientSecret` are required.
14
+ - `baseUrl` is optional.
15
+ - Resolution order for base URL:
16
+ 1. `baseUrl` passed to constructor
17
+ 2. `AUTHX_BASE_URL` environment variable
18
+ 3. `http://localhost:8080`
19
+
20
+ ## Quick start
21
+
22
+ ```javascript
23
+ const { AuthXClient, authMiddleware } = require("authx-sdk");
24
+
25
+ const authx = new AuthXClient({
26
+ clientId: process.env.AUTHX_CLIENT_ID,
27
+ clientSecret: process.env.AUTHX_CLIENT_SECRET,
28
+ baseUrl: process.env.AUTHX_BASE_URL, // optional
29
+ });
30
+
31
+ app.post("/api/signup", async (req, res) => {
32
+ const { email, username, password } = req.body;
33
+ const result = await authx.signup({ email, username, password });
34
+ res.json(result);
35
+ });
36
+
37
+ app.post("/api/login", async (req, res) => {
38
+ const { identifier, password } = req.body; // identifier can be email or username
39
+ const result = await authx.login({ identifier, password });
40
+ res.json(result);
41
+ });
42
+
43
+ const protect = authMiddleware(authx);
44
+ app.get("/api/me", protect, (req, res) => {
45
+ res.json({ email: req.user.email, userId: req.user.sub });
46
+ });
47
+ ```
48
+
49
+ ## API
50
+
51
+ - `new AuthXClient({ clientId, clientSecret, baseUrl? })`
52
+ - `authx.signup({ email, username, password })`
53
+ - `authx.login({ identifier, password })`
54
+ - `authx.introspect(token)`
55
+ - `authx.verifyEmail(token)`
56
+ - `authMiddleware(client)`
@@ -0,0 +1,47 @@
1
+ export interface AuthXConfig {
2
+ clientId: string;
3
+ clientSecret: string;
4
+ baseUrl?: string;
5
+ }
6
+ export interface SignupParams {
7
+ email: string;
8
+ username: string;
9
+ password: string;
10
+ }
11
+ export interface LoginParams {
12
+ identifier: string;
13
+ password: string;
14
+ }
15
+ export interface LoginResponse {
16
+ access_token: string;
17
+ token_type: string;
18
+ expires_in: number;
19
+ }
20
+ export interface IntrospectResponse {
21
+ active: boolean;
22
+ sub?: string;
23
+ email?: string;
24
+ exp?: number;
25
+ }
26
+ export interface ApiResponse {
27
+ message: string;
28
+ [key: string]: any;
29
+ }
30
+ export declare class AuthXError extends Error {
31
+ statusCode: number;
32
+ body: any;
33
+ constructor(message: string, statusCode: number, body?: any);
34
+ }
35
+ export declare class AuthXClient {
36
+ private clientId;
37
+ private clientSecret;
38
+ private baseUrl;
39
+ constructor(config: AuthXConfig);
40
+ private request;
41
+ signup(params: SignupParams): Promise<ApiResponse>;
42
+ login(params: LoginParams): Promise<LoginResponse>;
43
+ introspect(token: string): Promise<IntrospectResponse>;
44
+ verifyEmail(token: string): Promise<ApiResponse>;
45
+ }
46
+ export declare function authMiddleware(client: AuthXClient): (req: any, res: any, next: any) => Promise<any>;
47
+ export default AuthXClient;
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthXClient = exports.AuthXError = void 0;
4
+ exports.authMiddleware = authMiddleware;
5
+ class AuthXError extends Error {
6
+ constructor(message, statusCode, body) {
7
+ super(message);
8
+ this.name = "AuthXError";
9
+ this.statusCode = statusCode;
10
+ this.body = body;
11
+ }
12
+ }
13
+ exports.AuthXError = AuthXError;
14
+ class AuthXClient {
15
+ constructor(config) {
16
+ if (!config.clientId || !config.clientSecret) {
17
+ throw new Error("AuthX: clientId and clientSecret are required");
18
+ }
19
+ this.clientId = config.clientId;
20
+ this.clientSecret = config.clientSecret;
21
+ this.baseUrl = (config.baseUrl || process.env.AUTHX_BASE_URL || "http://localhost:8080").replace(/\/$/, "");
22
+ }
23
+ async request(path, body) {
24
+ const credentials = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64");
25
+ const res = await fetch(`${this.baseUrl}/api/v1/auth${path}`, {
26
+ method: "POST",
27
+ headers: {
28
+ "Content-Type": "application/json",
29
+ Authorization: `Basic ${credentials}`,
30
+ },
31
+ body: JSON.stringify(body),
32
+ });
33
+ const raw = await res.text();
34
+ let data = {};
35
+ try {
36
+ data = raw ? JSON.parse(raw) : {};
37
+ }
38
+ catch {
39
+ data = { message: raw };
40
+ }
41
+ if (!res.ok) {
42
+ throw new AuthXError(data.message || `AuthX request failed with status ${res.status}`, res.status, data);
43
+ }
44
+ return data;
45
+ }
46
+ async signup(params) {
47
+ return this.request("/signup", params);
48
+ }
49
+ async login(params) {
50
+ return this.request("/login", params);
51
+ }
52
+ async introspect(token) {
53
+ return this.request("/introspect", { token });
54
+ }
55
+ async verifyEmail(token) {
56
+ return this.request("/verify-email", { token });
57
+ }
58
+ }
59
+ exports.AuthXClient = AuthXClient;
60
+ function authMiddleware(client) {
61
+ return async (req, res, next) => {
62
+ const authHeader = req.headers?.authorization;
63
+ if (!authHeader?.startsWith("Bearer ")) {
64
+ return res.status(401).json({ error: "No token provided" });
65
+ }
66
+ const token = authHeader.substring(7);
67
+ try {
68
+ const result = await client.introspect(token);
69
+ if (!result.active) {
70
+ return res.status(401).json({ error: "Token expired or invalid" });
71
+ }
72
+ req.user = result;
73
+ next();
74
+ }
75
+ catch {
76
+ return res.status(401).json({ error: "Authentication failed" });
77
+ }
78
+ };
79
+ }
80
+ exports.default = AuthXClient;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "authx-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official AuthX Node.js SDK for backend authentication",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "prepublishOnly": "npm run build"
13
+ },
14
+ "keywords": [
15
+ "authx",
16
+ "authentication",
17
+ "jwt",
18
+ "sdk",
19
+ "auth"
20
+ ],
21
+ "license": "MIT",
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "dependencies": {},
26
+ "devDependencies": {
27
+ "typescript": "^5.0.0",
28
+ "@types/node": "^20.0.0"
29
+ }
30
+ }