@cedarjs/auth-dbauth-web 9.0.0-canary.1784 → 9.0.0-canary.3124

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,58 @@
1
+ /**
2
+ * Web client for dbAuth's OAuth routes.
3
+ *
4
+ * The OAuth handler lives in the same auth function as the rest of dbAuth,
5
+ * under the `/oauth` sub-path (e.g. `${RWJS_API_URL}/auth/oauth`). This
6
+ * module derives that base URL exactly the way `dbAuth.ts` does, and offers
7
+ * the same `dbAuthUrl` override affordance.
8
+ */
9
+ /**
10
+ * The three account flows the server distinguishes between, so a user can
11
+ * never accidentally create a duplicate account by logging in with a
12
+ * provider.
13
+ */
14
+ export type OAuthFlow = 'login' | 'signup' | 'link';
15
+ /**
16
+ * Error codes the OAuth handler's redirect can carry as `?error=<code>`.
17
+ */
18
+ export type OAuthErrorCode = 'unknown_provider' | 'invalid_state' | 'provider_error' | 'unknown_identity' | 'email_in_use' | 'identity_in_use' | 'not_authenticated' | 'flow_not_enabled' | 'cannot_unlink_last_identity' | 'untrusted_origin' | 'server_error';
19
+ export interface OAuthClientOptions {
20
+ /**
21
+ * Overrides the derived dbAuth base URL (the same override `dbAuth.ts`
22
+ * offers via `DbAuthClientArgs.dbAuthUrl`).
23
+ */
24
+ dbAuthUrl?: string;
25
+ }
26
+ export interface GetOAuthUrlOptions extends OAuthClientOptions {
27
+ /** Which account flow to start. Defaults to `'login'`. */
28
+ flow?: OAuthFlow;
29
+ }
30
+ export interface UnlinkOAuthProviderSuccess {
31
+ ok: true;
32
+ }
33
+ export interface UnlinkOAuthProviderFailure {
34
+ error: string;
35
+ }
36
+ export type UnlinkOAuthProviderResult = UnlinkOAuthProviderSuccess | UnlinkOAuthProviderFailure;
37
+ /**
38
+ * Builds the absolute URL for a provider's `authorize` route. Navigate the
39
+ * full page to this URL (e.g. `window.location.href = getOAuthUrl(...)`) —
40
+ * it starts a browser redirect flow, so it must not be `fetch`ed.
41
+ */
42
+ export declare function getOAuthUrl(provider: string, options?: GetOAuthUrlOptions): string;
43
+ /**
44
+ * Unlinks a provider identity from the current dbAuth session. Requires the
45
+ * dbAuth session cookie, so the request is sent with credentials included.
46
+ *
47
+ * The server only accepts this request from a trusted `Origin` (the app's
48
+ * own origin by default, or one listed in `trustedOrigins`/`cors.origin`) —
49
+ * see the `trustedOrigins` option on `OAuthHandlerOptions`.
50
+ */
51
+ export declare function unlinkOAuthProvider(provider: string, options?: OAuthClientOptions): Promise<UnlinkOAuthProviderResult>;
52
+ /**
53
+ * Extracts and narrows the `error` query param the OAuth handler's redirect
54
+ * carries after a failed flow. Returns `null` when there is no `error`
55
+ * param, or when its value isn't one of the known OAuth error codes.
56
+ */
57
+ export declare function getOAuthError(searchParams: URLSearchParams | string): OAuthErrorCode | null;
58
+ //# sourceMappingURL=oauth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oauth.d.ts","sourceRoot":"","sources":["../src/oauth.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEnD;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,kBAAkB,GAClB,eAAe,GACf,gBAAgB,GAChB,kBAAkB,GAClB,cAAc,GACd,iBAAiB,GACjB,mBAAmB,GACnB,kBAAkB,GAClB,6BAA6B,GAC7B,kBAAkB,GAClB,cAAc,CAAA;AAuBlB,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,kBAAmB,SAAQ,kBAAkB;IAC5D,0DAA0D;IAC1D,IAAI,CAAC,EAAE,SAAS,CAAA;CACjB;AAED,MAAM,WAAW,0BAA0B;IACzC,EAAE,EAAE,IAAI,CAAA;CACT;AAED,MAAM,WAAW,0BAA0B;IACzC,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,MAAM,yBAAyB,GACnC,0BAA0B,GAAG,0BAA0B,CAAA;AAMzD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,MAAM,CAKR;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,yBAAyB,CAAC,CAoBpC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,YAAY,EAAE,eAAe,GAAG,MAAM,GACrC,cAAc,GAAG,IAAI,CAavB"}
package/dist/oauth.js ADDED
@@ -0,0 +1,52 @@
1
+ const OAUTH_ERROR_CODES = [
2
+ "unknown_provider",
3
+ "invalid_state",
4
+ "provider_error",
5
+ "unknown_identity",
6
+ "email_in_use",
7
+ "identity_in_use",
8
+ "not_authenticated",
9
+ "flow_not_enabled",
10
+ "cannot_unlink_last_identity",
11
+ "untrusted_origin",
12
+ "server_error"
13
+ ];
14
+ function isOAuthErrorCode(value) {
15
+ return OAUTH_ERROR_CODES.includes(value);
16
+ }
17
+ function getDbAuthUrl(dbAuthUrl) {
18
+ return dbAuthUrl || `${RWJS_API_URL}/auth`;
19
+ }
20
+ function getOAuthUrl(provider, options) {
21
+ const flow = options?.flow ?? "login";
22
+ const baseUrl = getDbAuthUrl(options?.dbAuthUrl);
23
+ return `${baseUrl}/oauth/${encodeURIComponent(provider)}/authorize?flow=${flow}`;
24
+ }
25
+ async function unlinkOAuthProvider(provider, options) {
26
+ const baseUrl = getDbAuthUrl(options?.dbAuthUrl);
27
+ const response = await fetch(
28
+ `${baseUrl}/oauth/${encodeURIComponent(provider)}/unlink`,
29
+ {
30
+ method: "POST",
31
+ credentials: "include"
32
+ }
33
+ );
34
+ try {
35
+ return await response.json();
36
+ } catch {
37
+ return { error: "server_error" };
38
+ }
39
+ }
40
+ function getOAuthError(searchParams) {
41
+ const params = typeof searchParams === "string" ? new URLSearchParams(searchParams) : searchParams;
42
+ const error = params.get("error");
43
+ if (error && isOAuthErrorCode(error)) {
44
+ return error;
45
+ }
46
+ return null;
47
+ }
48
+ export {
49
+ getOAuthError,
50
+ getOAuthUrl,
51
+ unlinkOAuthProvider
52
+ };
@@ -2,7 +2,7 @@ export default class WebAuthnClient {
2
2
  authApiUrl: string;
3
3
  private getAuthApiUrl;
4
4
  setAuthApiUrl(authApiUrl?: string): void;
5
- isSupported(): Promise<any>;
5
+ isSupported(): Promise<boolean>;
6
6
  isEnabled(): boolean;
7
7
  private authenticationOptions;
8
8
  authenticate(): Promise<boolean>;
@@ -1 +1 @@
1
- {"version":3,"file":"webAuthn.d.ts","sourceRoot":"","sources":["../src/webAuthn.ts"],"names":[],"mappings":"AAqCA,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC,UAAU,SAAK;IAEf,OAAO,CAAC,aAAa;IAIrB,aAAa,CAAC,UAAU,CAAC,EAAE,MAAM;IAM3B,WAAW;IAKjB,SAAS;YAQK,qBAAqB;IAmC7B,YAAY;IA0ClB,OAAO,CAAC,mBAAmB;IA+BrB,QAAQ;CAwCf;AAED,MAAM,MAAM,kBAAkB,GAAG,OAAO,cAAc,CAAA"}
1
+ {"version":3,"file":"webAuthn.d.ts","sourceRoot":"","sources":["../src/webAuthn.ts"],"names":[],"mappings":"AAqCA,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC,UAAU,SAAK;IAEf,OAAO,CAAC,aAAa;IAIrB,aAAa,CAAC,UAAU,CAAC,EAAE,MAAM;IAM3B,WAAW;IAKjB,SAAS;YAQK,qBAAqB;IAmC7B,YAAY;IA4ClB,OAAO,CAAC,mBAAmB;IA+BrB,QAAQ;CAwCf;AAED,MAAM,MAAM,kBAAkB,GAAG,OAAO,cAAc,CAAA"}
package/dist/webAuthn.js CHANGED
@@ -84,7 +84,9 @@ class WebAuthnClient {
84
84
  const authOptions = await this.authenticationOptions();
85
85
  const { startAuthentication } = await import("@simplewebauthn/browser");
86
86
  try {
87
- const browserResponse = await startAuthentication(authOptions);
87
+ const browserResponse = await startAuthentication({
88
+ optionsJSON: authOptions
89
+ });
88
90
  const xhr = new XMLHttpRequest();
89
91
  xhr.withCredentials = true;
90
92
  xhr.open("POST", this.getAuthApiUrl(), false);
@@ -145,7 +147,7 @@ class WebAuthnClient {
145
147
  let regResponse;
146
148
  const { startRegistration } = await import("@simplewebauthn/browser");
147
149
  try {
148
- regResponse = await startRegistration(options);
150
+ regResponse = await startRegistration({ optionsJSON: options });
149
151
  } catch (e) {
150
152
  if (e.name === "InvalidStateError") {
151
153
  throw new WebAuthnAlreadyRegisteredError();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/auth-dbauth-web",
3
- "version": "9.0.0-canary.1784+baa47810a",
3
+ "version": "9.0.0-canary.3124",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -10,53 +10,58 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
- "import": {
13
+ "default": {
14
14
  "types": "./dist/index.d.ts",
15
15
  "default": "./dist/index.js"
16
- },
16
+ }
17
+ },
18
+ "./webAuthn": {
17
19
  "default": {
18
- "types": "./dist/cjs/index.d.ts",
19
- "default": "./dist/cjs/index.js"
20
+ "types": "./dist/webAuthn.d.ts",
21
+ "default": "./dist/webAuthn.js"
22
+ }
23
+ },
24
+ "./oauth": {
25
+ "default": {
26
+ "types": "./dist/oauth.d.ts",
27
+ "default": "./dist/oauth.js"
20
28
  }
21
29
  }
22
30
  },
23
- "main": "./dist/cjs/index.js",
24
- "module": "./dist/index.js",
31
+ "main": "./dist/index.js",
25
32
  "types": "./dist/index.d.ts",
26
33
  "files": [
27
- "dist",
28
- "webAuthn"
34
+ "dist"
29
35
  ],
30
36
  "scripts": {
31
- "build": "tsx ./build.ts",
37
+ "build": "node ./build.ts",
32
38
  "build:pack": "yarn pack -o cedarjs-auth-dbauth-web.tgz",
33
39
  "build:types": "tsc --build --verbose ./tsconfig.build.json",
34
- "build:types-cjs": "tsc --build --verbose ./tsconfig.cjs.json",
35
40
  "build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
36
- "check:attw": "yarn rw-fwtools-attw",
41
+ "check:attw": "yarn cedar-fwtools-attw",
37
42
  "check:package": "concurrently npm:check:attw yarn:publint",
38
43
  "prepublishOnly": "NODE_ENV=production yarn build",
39
44
  "test": "vitest run",
40
45
  "test:watch": "vitest watch"
41
46
  },
42
47
  "dependencies": {
43
- "@cedarjs/auth": "9.0.0-canary.1784",
44
- "@simplewebauthn/browser": "9.0.1"
48
+ "@cedarjs/auth": "9.0.0-canary.3124",
49
+ "@simplewebauthn/browser": "13.3.0"
45
50
  },
46
51
  "devDependencies": {
47
- "@arethetypeswrong/cli": "0.18.2",
48
- "@cedarjs/framework-tools": "9.0.0-canary.1784",
49
- "@simplewebauthn/types": "9.0.1",
50
- "@types/react": "^18.2.55",
51
- "concurrently": "9.2.1",
52
- "publint": "0.3.17",
53
- "react": "19.2.3",
54
- "tsx": "4.21.0",
52
+ "@cedarjs/framework-tools": "9.0.0-canary.3124",
53
+ "@types/react": "^19.2.0",
54
+ "concurrently": "9.2.4",
55
+ "publint": "0.3.24",
56
+ "react": "19.2.8",
55
57
  "typescript": "5.9.3",
56
- "vitest": "3.2.4"
58
+ "vitest": "4.1.11"
59
+ },
60
+ "engines": {
61
+ "node": ">=24"
57
62
  },
58
63
  "publishConfig": {
59
64
  "access": "public"
60
65
  },
61
- "gitHead": "baa47810a7defb73de27fdf513b815d679509b5e"
66
+ "gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
62
67
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Cedar
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.
@@ -1,42 +0,0 @@
1
- import type { CurrentUser } from '@cedarjs/auth';
2
- import type { WebAuthnClientType } from './webAuthn.js';
3
- export interface LoginAttributes {
4
- username: string;
5
- password: string;
6
- }
7
- export interface ResetPasswordAttributes {
8
- resetToken: string;
9
- password: string;
10
- }
11
- export type SignupAttributes = Record<string, unknown> & LoginAttributes;
12
- export declare function createAuth(dbAuthClient: ReturnType<typeof createDbAuthClient>, customProviderHooks?: {
13
- useCurrentUser?: () => Promise<CurrentUser>;
14
- useHasRole?: (currentUser: CurrentUser | null) => (rolesToCheck: string | string[]) => boolean;
15
- }): {
16
- AuthContext: import("react").Context<import("@cedarjs/auth").AuthContextInterface<string | (() => void), LoginAttributes, any, unknown, boolean, SignupAttributes, any, any, ResetPasswordAttributes, any, any, import("./webAuthn.js").default> | undefined>;
17
- AuthProvider: ({ children }: import("@cedarjs/auth").AuthProviderProps) => import("react").JSX.Element;
18
- useAuth: () => import("@cedarjs/auth").AuthContextInterface<string | (() => void), LoginAttributes, any, unknown, boolean, SignupAttributes, any, any, ResetPasswordAttributes, any, any, import("./webAuthn.js").default>;
19
- };
20
- export interface DbAuthClientArgs {
21
- webAuthn?: InstanceType<WebAuthnClientType>;
22
- dbAuthUrl?: string;
23
- fetchConfig?: {
24
- credentials?: 'include' | 'same-origin';
25
- };
26
- middleware?: boolean;
27
- }
28
- export declare function createDbAuthClient({ webAuthn, dbAuthUrl, fetchConfig, middleware, }?: DbAuthClientArgs): {
29
- type: string;
30
- client: import("./webAuthn.js").default | undefined;
31
- login: ({ username, password }: LoginAttributes) => Promise<any>;
32
- logout: () => Promise<boolean>;
33
- signup: (attributes: SignupAttributes) => Promise<any>;
34
- getToken: () => Promise<string | null>;
35
- getUserMetadata: () => Promise<string | (() => void) | null>;
36
- forgotPassword: (username: string) => Promise<any>;
37
- resetPassword: (attributes: ResetPasswordAttributes) => Promise<any>;
38
- validateResetToken: (resetToken: string | null) => Promise<any>;
39
- getAuthUrl: () => string;
40
- middlewareAuthEnabled: boolean;
41
- };
42
- //# sourceMappingURL=dbAuth.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dbAuth.d.ts","sourceRoot":"","sources":["../../src/dbAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAuB,MAAM,eAAe,CAAA;AAMrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEvD,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,eAAe,CAAA;AAkBxE,wBAAgB,UAAU,CACxB,YAAY,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,EACnD,mBAAmB,CAAC,EAAE;IACpB,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,CAAA;IAC3C,UAAU,CAAC,EAAE,CACX,WAAW,EAAE,WAAW,GAAG,IAAI,KAC5B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,OAAO,CAAA;CAClD;;;;EAOF;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,YAAY,CAAC,kBAAkB,CAAC,CAAA;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE;QACZ,WAAW,CAAC,EAAE,SAAS,GAAG,aAAa,CAAA;KACxC,CAAA;IACD,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,wBAAgB,kBAAkB,CAAC,EACjC,QAAQ,EACR,SAAS,EACT,WAAW,EACX,UAA4C,GAC7C,GAAE,gBAAqB;;;oCA6EuB,eAAe;;yBAgC1B,gBAAgB;;;+BA9EV,MAAM;gCAmEL,uBAAuB;qCAsBlB,MAAM,GAAG,IAAI;;;EAoC5D"}
@@ -1,170 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var dbAuth_exports = {};
20
- __export(dbAuth_exports, {
21
- createAuth: () => createAuth,
22
- createDbAuthClient: () => createDbAuthClient
23
- });
24
- module.exports = __toCommonJS(dbAuth_exports);
25
- var import_auth = require("@cedarjs/auth");
26
- const TOKEN_CACHE_TIME = 5e3;
27
- function createMiddlewareAuth(dbAuthClient, customProviderHooks) {
28
- return (0, import_auth.createAuthentication)(dbAuthClient, {
29
- ...customProviderHooks,
30
- useCurrentUser: customProviderHooks?.useCurrentUser ?? (() => (0, import_auth.getCurrentUserFromMiddleware)(dbAuthClient.getAuthUrl()))
31
- });
32
- }
33
- function createAuth(dbAuthClient, customProviderHooks) {
34
- if (dbAuthClient.middlewareAuthEnabled) {
35
- return createMiddlewareAuth(dbAuthClient, customProviderHooks);
36
- }
37
- return (0, import_auth.createAuthentication)(dbAuthClient, customProviderHooks);
38
- }
39
- function createDbAuthClient({
40
- webAuthn,
41
- dbAuthUrl,
42
- fetchConfig,
43
- middleware = RWJS_ENV.RWJS_EXP_STREAMING_SSR
44
- } = {}) {
45
- const credentials = fetchConfig?.credentials || "same-origin";
46
- webAuthn?.setAuthApiUrl(dbAuthUrl);
47
- let getTokenPromise;
48
- let lastTokenCheckAt = /* @__PURE__ */ new Date("1970-01-01T00:00:00");
49
- let cachedToken;
50
- const getDbAuthUrl = () => {
51
- if (dbAuthUrl) {
52
- return dbAuthUrl;
53
- }
54
- return middleware ? `/middleware/dbauth` : `${RWJS_API_URL}/auth`;
55
- };
56
- const resetAndFetch = async (...params) => {
57
- resetTokenCache();
58
- return fetch(...params);
59
- };
60
- const isTokenCacheExpired = () => {
61
- const now = /* @__PURE__ */ new Date();
62
- return now.getTime() - lastTokenCheckAt.getTime() > TOKEN_CACHE_TIME;
63
- };
64
- const resetTokenCache = () => {
65
- lastTokenCheckAt = /* @__PURE__ */ new Date("1970-01-01T00:00:00");
66
- cachedToken = null;
67
- };
68
- const forgotPassword = async (username) => {
69
- const response = await resetAndFetch(getDbAuthUrl(), {
70
- credentials,
71
- method: "POST",
72
- headers: { "Content-Type": "application/json" },
73
- body: JSON.stringify({ username, method: "forgotPassword" })
74
- });
75
- return response.json();
76
- };
77
- const getToken = async () => {
78
- if (middleware) {
79
- return null;
80
- }
81
- if (getTokenPromise) {
82
- return getTokenPromise;
83
- }
84
- if (isTokenCacheExpired()) {
85
- getTokenPromise = fetch(`${getDbAuthUrl()}?method=getToken`, {
86
- credentials
87
- }).then((response) => response.text()).then((tokenText) => {
88
- lastTokenCheckAt = /* @__PURE__ */ new Date();
89
- cachedToken = tokenText.length === 0 ? null : tokenText;
90
- return cachedToken;
91
- }).catch(() => {
92
- return null;
93
- }).finally(() => {
94
- getTokenPromise = null;
95
- });
96
- return getTokenPromise;
97
- }
98
- return cachedToken;
99
- };
100
- const login = async ({ username, password }) => {
101
- const response = await resetAndFetch(getDbAuthUrl(), {
102
- credentials,
103
- method: "POST",
104
- headers: { "Content-Type": "application/json" },
105
- body: JSON.stringify({ username, password, method: "login" })
106
- });
107
- return response.json();
108
- };
109
- const logout = async () => {
110
- await resetAndFetch(getDbAuthUrl(), {
111
- credentials,
112
- method: "POST",
113
- body: JSON.stringify({ method: "logout" })
114
- });
115
- return true;
116
- };
117
- const resetPassword = async (attributes) => {
118
- const response = await resetAndFetch(getDbAuthUrl(), {
119
- credentials,
120
- method: "POST",
121
- headers: { "Content-Type": "application/json" },
122
- body: JSON.stringify({ ...attributes, method: "resetPassword" })
123
- });
124
- return response.json();
125
- };
126
- const signup = async (attributes) => {
127
- const response = await resetAndFetch(getDbAuthUrl(), {
128
- credentials,
129
- method: "POST",
130
- headers: { "Content-Type": "application/json" },
131
- body: JSON.stringify({ ...attributes, method: "signup" })
132
- });
133
- return response.json();
134
- };
135
- const validateResetToken = async (resetToken) => {
136
- const response = await resetAndFetch(getDbAuthUrl(), {
137
- credentials,
138
- method: "POST",
139
- headers: { "Content-Type": "application/json" },
140
- body: JSON.stringify({ resetToken, method: "validateResetToken" })
141
- });
142
- return response.json();
143
- };
144
- const getUserMetadata = async () => {
145
- return middleware ? () => {
146
- } : getToken();
147
- };
148
- return {
149
- type: "dbAuth",
150
- client: webAuthn,
151
- login,
152
- logout,
153
- signup,
154
- getToken,
155
- getUserMetadata,
156
- forgotPassword,
157
- resetPassword,
158
- validateResetToken,
159
- // New methods for middleware auth:
160
- // This is so we can get the dbAuthUrl in getCurrentUserFromMiddleware
161
- getAuthUrl: getDbAuthUrl,
162
- // This is so that we can skip fetching getCurrentUser in reauthenticate
163
- middlewareAuthEnabled: middleware
164
- };
165
- }
166
- // Annotate the CommonJS export names for ESM import in node:
167
- 0 && (module.exports = {
168
- createAuth,
169
- createDbAuthClient
170
- });
@@ -1,2 +0,0 @@
1
- export * from './dbAuth.js';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
package/dist/cjs/index.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var src_exports = {};
17
- module.exports = __toCommonJS(src_exports);
18
- __reExport(src_exports, require("./dbAuth.js"), module.exports);
19
- // Annotate the CommonJS export names for ESM import in node:
20
- 0 && (module.exports = {
21
- ...require("./dbAuth.js")
22
- });
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,13 +0,0 @@
1
- export default class WebAuthnClient {
2
- authApiUrl: string;
3
- private getAuthApiUrl;
4
- setAuthApiUrl(authApiUrl?: string): void;
5
- isSupported(): Promise<any>;
6
- isEnabled(): boolean;
7
- private authenticationOptions;
8
- authenticate(): Promise<boolean>;
9
- private registrationOptions;
10
- register(): Promise<boolean>;
11
- }
12
- export type WebAuthnClientType = typeof WebAuthnClient;
13
- //# sourceMappingURL=webAuthn.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"webAuthn.d.ts","sourceRoot":"","sources":["../../src/webAuthn.ts"],"names":[],"mappings":"AAqCA,MAAM,CAAC,OAAO,OAAO,cAAc;IACjC,UAAU,SAAK;IAEf,OAAO,CAAC,aAAa;IAIrB,aAAa,CAAC,UAAU,CAAC,EAAE,MAAM;IAM3B,WAAW;IAKjB,SAAS;YAQK,qBAAqB;IAmC7B,YAAY;IA0ClB,OAAO,CAAC,mBAAmB;IA+BrB,QAAQ;CAwCf;AAED,MAAM,MAAM,kBAAkB,GAAG,OAAO,cAAc,CAAA"}
@@ -1,210 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var webAuthn_exports = {};
30
- __export(webAuthn_exports, {
31
- default: () => WebAuthnClient
32
- });
33
- module.exports = __toCommonJS(webAuthn_exports);
34
- class WebAuthnRegistrationError extends Error {
35
- constructor(message) {
36
- super(message);
37
- this.name = "WebAuthnRegistrationError";
38
- }
39
- }
40
- class WebAuthnAuthenticationError extends Error {
41
- constructor(message) {
42
- super(message);
43
- this.name = "WebAuthnAuthenticationError";
44
- }
45
- }
46
- class WebAuthnAlreadyRegisteredError extends WebAuthnRegistrationError {
47
- constructor() {
48
- super("This device is already registered");
49
- this.name = "WebAuthnAlreadyRegisteredError";
50
- }
51
- }
52
- class WebAuthnDeviceNotFoundError extends WebAuthnAuthenticationError {
53
- constructor() {
54
- super("WebAuthn device not found");
55
- this.name = "WebAuthnDeviceNotFoundError";
56
- }
57
- }
58
- class WebAuthnNoAuthenticatorError extends WebAuthnAuthenticationError {
59
- constructor() {
60
- super(
61
- "This device was not recognized. Use username/password login, or if you're using iOS you can try reloading this page"
62
- );
63
- this.name = "WebAuthnNoAuthenticatorError";
64
- }
65
- }
66
- class WebAuthnClient {
67
- authApiUrl = "";
68
- getAuthApiUrl() {
69
- return this.authApiUrl || `${RWJS_API_URL}/auth`;
70
- }
71
- setAuthApiUrl(authApiUrl) {
72
- if (authApiUrl) {
73
- this.authApiUrl = authApiUrl;
74
- }
75
- }
76
- async isSupported() {
77
- const { browserSupportsWebAuthn } = await import("@simplewebauthn/browser");
78
- return browserSupportsWebAuthn();
79
- }
80
- isEnabled() {
81
- if (typeof window === "undefined") {
82
- return false;
83
- }
84
- return !!/\bwebAuthn\b/.test(document.cookie);
85
- }
86
- async authenticationOptions() {
87
- let options;
88
- try {
89
- const xhr = new XMLHttpRequest();
90
- xhr.withCredentials = true;
91
- xhr.open(
92
- "GET",
93
- `${this.getAuthApiUrl()}?method=webAuthnAuthOptions`,
94
- false
95
- );
96
- xhr.setRequestHeader("content-type", "application/json");
97
- xhr.send(null);
98
- options = JSON.parse(xhr.responseText);
99
- if (xhr.status !== 200) {
100
- if (options.error?.match(/username and password/)) {
101
- throw new WebAuthnDeviceNotFoundError();
102
- } else {
103
- throw new WebAuthnAuthenticationError(
104
- `Could not start authentication: ${options.error}`
105
- );
106
- }
107
- }
108
- } catch (e) {
109
- console.error(e.message);
110
- throw new WebAuthnAuthenticationError(
111
- `Could not start authentication: ${e.message}`
112
- );
113
- }
114
- return options;
115
- }
116
- async authenticate() {
117
- const authOptions = await this.authenticationOptions();
118
- const { startAuthentication } = await import("@simplewebauthn/browser");
119
- try {
120
- const browserResponse = await startAuthentication(authOptions);
121
- const xhr = new XMLHttpRequest();
122
- xhr.withCredentials = true;
123
- xhr.open("POST", this.getAuthApiUrl(), false);
124
- xhr.setRequestHeader("content-type", "application/json");
125
- xhr.send(
126
- JSON.stringify({
127
- method: "webAuthnAuthenticate",
128
- ...browserResponse
129
- })
130
- );
131
- const options = JSON.parse(xhr.responseText);
132
- if (xhr.status !== 200) {
133
- throw new WebAuthnAuthenticationError(
134
- `Could not complete authentication: ${options.error}`
135
- );
136
- }
137
- } catch (e) {
138
- if (e.message.match(
139
- /No available authenticator recognized any of the allowed credentials/
140
- )) {
141
- throw new WebAuthnNoAuthenticatorError();
142
- } else {
143
- throw new WebAuthnAuthenticationError(
144
- `Error while authenticating: ${e.message}`
145
- );
146
- }
147
- }
148
- return true;
149
- }
150
- registrationOptions() {
151
- let options;
152
- try {
153
- const xhr = new XMLHttpRequest();
154
- xhr.withCredentials = true;
155
- xhr.open(
156
- "GET",
157
- `${this.getAuthApiUrl()}?method=webAuthnRegOptions`,
158
- false
159
- );
160
- xhr.setRequestHeader("content-type", "application/json");
161
- xhr.send(null);
162
- options = JSON.parse(xhr.responseText);
163
- if (xhr.status !== 200) {
164
- throw new WebAuthnRegistrationError(
165
- `Could not start registration: ${options.error}`
166
- );
167
- }
168
- } catch (e) {
169
- console.error(e);
170
- throw new WebAuthnRegistrationError(
171
- `Could not start registration: ${e.message}`
172
- );
173
- }
174
- return options;
175
- }
176
- async register() {
177
- const options = await this.registrationOptions();
178
- let regResponse;
179
- const { startRegistration } = await import("@simplewebauthn/browser");
180
- try {
181
- regResponse = await startRegistration(options);
182
- } catch (e) {
183
- if (e.name === "InvalidStateError") {
184
- throw new WebAuthnAlreadyRegisteredError();
185
- } else {
186
- throw new WebAuthnRegistrationError(
187
- `Error while registering: ${e.message}`
188
- );
189
- }
190
- }
191
- try {
192
- const xhr = new XMLHttpRequest();
193
- xhr.withCredentials = true;
194
- xhr.open("POST", this.getAuthApiUrl(), false);
195
- xhr.setRequestHeader("content-type", "application/json");
196
- xhr.send(JSON.stringify({ method: "webAuthnRegister", ...regResponse }));
197
- const options2 = JSON.parse(xhr.responseText);
198
- if (xhr.status !== 200) {
199
- throw new WebAuthnRegistrationError(
200
- `Could not complete registration: ${options2.error}`
201
- );
202
- }
203
- } catch (e) {
204
- throw new WebAuthnRegistrationError(
205
- `Error while registering: ${e.message}`
206
- );
207
- }
208
- return true;
209
- }
210
- }
package/webAuthn/index.js DELETED
@@ -1,2 +0,0 @@
1
- // For CommonJS environments, point to the CJS build
2
- module.exports = require('../dist/cjs/webAuthn')
@@ -1,4 +0,0 @@
1
- {
2
- "main": "./index.js",
3
- "types": "../dist/cjs/webAuthn.d.ts"
4
- }