@nexusrt/nexus-auth 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,277 @@
1
+ # JobTrk Auth SDK
2
+
3
+ A universal, framework-agnostic TypeScript SDK for the JobTrk authentication system.
4
+ Works in React, Vue, Svelte, Angular, vanilla JS — anywhere that runs in a browser.
5
+
6
+ ---
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install jobtrk-auth-sdk
12
+ ```
13
+
14
+ ---
15
+
16
+ ## Quick Start
17
+
18
+ ```ts
19
+ import { createAuthClient, SignInStatus } from "jobtrk-auth-sdk";
20
+
21
+ const auth = createAuthClient({
22
+ baseUrl: "https://auth.yourapp.com", // defaults to https://auth.jobtrk.com
23
+ });
24
+ ```
25
+
26
+ > **`createAuthClient` is the recommended way to use the SDK.**
27
+ > It pre-binds `baseUrl` to every method so you never need to pass it manually.
28
+
29
+ ---
30
+
31
+ ## Session Modes
32
+
33
+ The SDK supports two ways to persist a login:
34
+
35
+ | Mode | How it works | When to use |
36
+ |---|---|---|
37
+ | **JWT** | Server returns an `access_token` + sets an HttpOnly refresh-token cookie | Stateless APIs, microservices |
38
+ | **Session** | Server sets an HttpOnly session cookie | Traditional server-rendered apps |
39
+
40
+ In both modes an **access token** is available so you can authenticate with downstream services.
41
+
42
+ ### Restore session on app startup
43
+
44
+ ```ts
45
+ // JWT mode — uses the HttpOnly refresh-token cookie to get a new access token
46
+ const user = await auth.token.refresh();
47
+ if (user) {
48
+ console.log("Logged in as", user.email);
49
+ }
50
+
51
+ // Session mode — read the current user from the server
52
+ const user = await auth.session.getUser();
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Social Sign-In
58
+
59
+ Redirects the browser to the provider's OAuth flow.
60
+
61
+ ```ts
62
+ auth.social.signInWithGoogle();
63
+ auth.social.signInWithGithub();
64
+ auth.social.signInWithLinkedIn();
65
+ auth.social.signInWithOkta();
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Email + Password
71
+
72
+ ### Sign up
73
+
74
+ ```ts
75
+ await auth.email.signUp({ email, password });
76
+ ```
77
+
78
+ ### Sign in (no MFA)
79
+
80
+ ```ts
81
+ const result = await auth.email.signIn({ email, password });
82
+ // result.access_token is set — user is signed in
83
+ ```
84
+
85
+ ### Sign in with Email MFA
86
+
87
+ ```ts
88
+ const result = await auth.email.signIn({ email, password });
89
+
90
+ if (result.status === SignInStatus.MFA_REQUIRED) {
91
+ const sessionId = result.mfa!;
92
+
93
+ // Prompt the user for the code emailed to them
94
+ await auth.email.verifyMfa({ sessionId, code: userEnteredCode });
95
+ }
96
+ ```
97
+
98
+ ### Sign in with TOTP
99
+
100
+ ```ts
101
+ const result = await auth.email.signIn({ email, password });
102
+
103
+ if (result.status === SignInStatus.TOTP_REQUIRED) {
104
+ const sessionId = result.mfa!;
105
+
106
+ // Prompt the user for their authenticator app code
107
+ await auth.totp.verifySignIn({ sessionId, code: userEnteredCode });
108
+ }
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Email Magic Link
114
+
115
+ ```ts
116
+ // 1. Send the magic link
117
+ const { mfa: sessionId } = await auth.magicLink.send(email);
118
+
119
+ // 2. User receives an email with a code — verify it
120
+ await auth.magicLink.verify({ sessionId, code: userEnteredCode });
121
+ ```
122
+
123
+ ---
124
+
125
+ ## SSO (Multi-Tenant)
126
+
127
+ ### Organisation admin — register your IDP once
128
+
129
+ ```ts
130
+ await auth.sso.registerProvider({
131
+ providerName: "Acme Corp",
132
+ providerEndEmail: "acme.com",
133
+ clientId: "...",
134
+ clientSecret: "...",
135
+ issuer: "https://idp.acme.com",
136
+ callbackUrl: "https://yourapp.com/auth/callback",
137
+ });
138
+ ```
139
+
140
+ ### End user — sign in via their org's IDP
141
+
142
+ ```ts
143
+ const { auth_url } = await auth.sso.signIn(email);
144
+ window.location.href = auth_url; // redirect to org's IDP
145
+ ```
146
+
147
+ ---
148
+
149
+ ## Password Management
150
+
151
+ ### Reset password (authenticated user)
152
+
153
+ ```ts
154
+ await auth.password.reset({
155
+ oldPassword: "current-password",
156
+ newPassword: "new-password",
157
+ });
158
+ ```
159
+
160
+ ### Forgot password
161
+
162
+ ```ts
163
+ // 1. Send reset code to email
164
+ const { mfa: sessionId } = await auth.password.forgot(email);
165
+
166
+ // 2. User enters code from email
167
+ await auth.password.confirmForgot({
168
+ sessionId,
169
+ code: userEnteredCode,
170
+ password: newPassword,
171
+ });
172
+ ```
173
+
174
+ ---
175
+
176
+ ## TOTP Setup
177
+
178
+ ```ts
179
+ // 1. Start registration — display the QR code / secret to the user
180
+ const { totp } = await auth.totp.setup();
181
+ // totp = QR code data or secret string — render as a QR code
182
+
183
+ // 2. User scans with their authenticator app and enters the first code
184
+ await auth.totp.confirmSetup(userEnteredCode);
185
+
186
+ // Remove TOTP from account
187
+ await auth.totp.delete();
188
+ ```
189
+
190
+ ---
191
+
192
+ ## Email MFA Management
193
+
194
+ ```ts
195
+ await auth.emailMfa.create(); // enable email MFA
196
+ await auth.emailMfa.delete(); // disable email MFA
197
+ ```
198
+
199
+ ---
200
+
201
+ ## Logout
202
+
203
+ ```ts
204
+ await auth.logout(); // sign out of current session
205
+ await auth.logoutAllSessions(); // sign out of all devices
206
+ ```
207
+
208
+ ---
209
+
210
+ ## Status Codes
211
+
212
+ Import `SignInStatus` to compare against sign-in responses:
213
+
214
+ ```ts
215
+ import { SignInStatus } from "jobtrk-auth-sdk";
216
+
217
+ SignInStatus.MFA_REQUIRED // Email OTP challenge required
218
+ SignInStatus.TOTP_REQUIRED // TOTP challenge required
219
+ SignInStatus.MAGIC_LINK // Magic link sent
220
+ SignInStatus.RESET_PASSWORD // Password reset code sent
221
+ SignInStatus.SUCCESS // No MFA — user is signed in
222
+ ```
223
+
224
+ ---
225
+
226
+ ## Error Handling
227
+
228
+ All methods throw an `AuthError` on failure:
229
+
230
+ ```ts
231
+ import { AuthError } from "jobtrk-auth-sdk";
232
+
233
+ try {
234
+ await auth.email.signIn({ email, password });
235
+ } catch (err) {
236
+ if (err instanceof AuthError) {
237
+ console.error(err.message); // human-readable message
238
+ console.error(err.status); // HTTP status code
239
+ console.error(err.body); // raw response body
240
+ }
241
+ }
242
+ ```
243
+
244
+ ---
245
+
246
+ ## React Example
247
+
248
+ ```tsx
249
+ import { createAuthClient, SignInStatus, AuthError } from "jobtrk-auth-sdk";
250
+ import { useEffect, useState } from "react";
251
+
252
+ const auth = createAuthClient({ baseUrl: "https://auth.yourapp.com" });
253
+
254
+ export function useAuth() {
255
+ const [user, setUser] = useState(null);
256
+
257
+ // Restore session on mount
258
+ useEffect(() => {
259
+ auth.token.refresh().then(setUser);
260
+ }, []);
261
+
262
+ const signIn = async (email: string, password: string) => {
263
+ const result = await auth.email.signIn({ email, password });
264
+ if (!result.status) {
265
+ setUser(auth.token.getUserFromToken());
266
+ }
267
+ return result; // caller handles MFA branching
268
+ };
269
+
270
+ const logout = async () => {
271
+ await auth.logout();
272
+ setUser(null);
273
+ };
274
+
275
+ return { user, signIn, logout, auth };
276
+ }
277
+ ```
@@ -0,0 +1,106 @@
1
+ import { TokenManager } from "./methods/token";
2
+ import { SessionManager } from "./methods/session";
3
+ import { EmailPasswordAuth } from "./methods/emailPassword";
4
+ import { MagicLinkAuth } from "./methods/magicLink";
5
+ import { TotpAuth } from "./methods/totp";
6
+ import { PasswordAuth } from "./methods/password";
7
+ import { SsoAuth } from "./methods/sso";
8
+ import { EmailMfaManager } from "./methods/emailMfa";
9
+ import { SocialAuth } from "./providers/social";
10
+ import type { AuthClientConfig } from "./types";
11
+ /**
12
+ * AuthClient
13
+ *
14
+ * The single entry point for the JobTrk auth SDK.
15
+ * Framework-agnostic — works in React, Vue, Svelte, vanilla JS, etc.
16
+ *
17
+ * ── Quick start ──────────────────────────────────────────────────────
18
+ *
19
+ * ```ts
20
+ * import { AuthClient, SignInStatus } from "jobtrk-auth-sdk";
21
+ *
22
+ * const auth = new AuthClient({ baseUrl: "https://auth.yourapp.com" });
23
+ *
24
+ * // Restore session on app startup
25
+ * const user = await auth.token.refresh();
26
+ *
27
+ * // Social sign-in
28
+ * auth.social.signInWithGoogle();
29
+ *
30
+ * // Email + password sign-in (with MFA branching)
31
+ * const result = await auth.email.signIn({ email, password });
32
+ * if (result.status === SignInStatus.MFA_REQUIRED) { ... }
33
+ * if (result.status === SignInStatus.TOTP_REQUIRED) { ... }
34
+ *
35
+ * // Magic link
36
+ * const { mfa: sessionId } = await auth.magicLink.send(email);
37
+ * await auth.magicLink.verify({ sessionId, code });
38
+ *
39
+ * // SSO
40
+ * const { auth_url } = await auth.sso.signIn(email);
41
+ * window.location.href = auth_url;
42
+ * ```
43
+ */
44
+ export declare class AuthClient {
45
+ private readonly baseUrl;
46
+ /** JWT access token management and refresh */
47
+ readonly token: TokenManager;
48
+ /** Session-based auth helpers (getUser, verifySession) */
49
+ readonly session: SessionManager;
50
+ /** Social / OAuth redirects (Google, GitHub, LinkedIn, Okta) */
51
+ readonly social: SocialAuth;
52
+ /** Email + password sign-up, sign-in, and MFA verify */
53
+ readonly email: EmailPasswordAuth;
54
+ /** Email magic-link send and verify */
55
+ readonly magicLink: MagicLinkAuth;
56
+ /** TOTP setup, confirm, sign-in verify, and delete */
57
+ readonly totp: TotpAuth;
58
+ /** Email MFA enable / disable */
59
+ readonly emailMfa: EmailMfaManager;
60
+ /** Password reset (authenticated) and forgot-password flow */
61
+ readonly password: PasswordAuth;
62
+ /** Multi-tenant SSO provider registration and sign-in */
63
+ readonly sso: SsoAuth;
64
+ constructor(config?: AuthClientConfig);
65
+ /**
66
+ * Signs the user out of the current session/device.
67
+ * Clears the in-memory access token after a successful server response.
68
+ */
69
+ logout(): Promise<void>;
70
+ /**
71
+ * Signs the user out of ALL active sessions / devices.
72
+ * Clears the in-memory access token after a successful server response.
73
+ */
74
+ logoutAllSessions(): Promise<void>;
75
+ }
76
+ type BoundModule<T extends object> = {
77
+ [K in keyof T]: T[K] extends (baseUrl: string, ...args: infer A) => infer R ? (...args: A) => R : T[K];
78
+ };
79
+ /**
80
+ * Creates an `AuthClient` where all sub-module methods have the `baseUrl`
81
+ * argument pre-bound. This is the recommended way to instantiate the SDK.
82
+ *
83
+ * ```ts
84
+ * const auth = createAuthClient({ baseUrl: "https://auth.yourapp.com" });
85
+ *
86
+ * // No need to pass baseUrl to individual methods:
87
+ * await auth.email.signIn({ email, password });
88
+ * await auth.token.refresh();
89
+ * ```
90
+ */
91
+ export declare function createAuthClient(config?: AuthClientConfig): {
92
+ token: BoundModule<TokenManager>;
93
+ session: BoundModule<SessionManager>;
94
+ email: BoundModule<EmailPasswordAuth>;
95
+ magicLink: BoundModule<MagicLinkAuth>;
96
+ totp: BoundModule<TotpAuth>;
97
+ emailMfa: BoundModule<EmailMfaManager>;
98
+ password: BoundModule<PasswordAuth>;
99
+ sso: BoundModule<SsoAuth>;
100
+ social: SocialAuth;
101
+ logout: () => Promise<void>;
102
+ logoutAllSessions: () => Promise<void>;
103
+ };
104
+ export type BoundAuthClient = ReturnType<typeof createAuthClient>;
105
+ export {};
106
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,OAAO,KAAK,EAAE,gBA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G,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
package/dist/http.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Lightweight fetch wrapper used internally by the SDK.
3
+ * All requests automatically include credentials (cookies) for session support.
4
+ */
5
+ export declare function request<T>(url: string, options?: RequestInit): Promise<T>;
6
+ export declare class AuthError extends Error {
7
+ readonly status: number;
8
+ readonly body: unknown;
9
+ constructor(message: string, status: number, body?: unknown);
10
+ }
11
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC7B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,GACxB,OAAO,CAAC,CAAC,CAAC,CAoBZ;AAMD,qBAAa,SAAU,SAAQ,KAAK;aAGhB,MAAM,EAAE,MAAM;aACd,IAAI,EAAE,OAAO;gBAF7B,OAAO,EAAE,MAAM,EACC,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,OAAc;CAKvC"}