UNPKG

@better-auth-ui/core

Version:

Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.

463 lines (462 loc) • 13.3 kB
import { t as e } from "../../auth-query-keys-B_bcsA3s.js"; import { r as t } from "../../auth-query-retry-Dyv0hsPy.js"; import { t as n } from "../../create-auth-plugin-BXWyolC8.js"; import { skipToken as r } from "@tanstack/query-core"; //#region src/plugins/oauth-provider/oauth-provider-mutation-keys.ts var i = { all: ["auth", "oauthProvider"], consent: [ "auth", "oauthProvider", "consent" ], continue: [ "auth", "oauthProvider", "continue" ], deleteConsent: [ "auth", "oauthProvider", "deleteConsent" ], createClient: [ "auth", "oauthProvider", "createClient" ], updateClient: [ "auth", "oauthProvider", "updateClient" ], deleteClient: [ "auth", "oauthProvider", "deleteClient" ], rotateClientSecret: [ "auth", "oauthProvider", "rotateClientSecret" ], setClientDisabled: [ "auth", "oauthProvider", "setClientDisabled" ] }, a = { all: ["auth", "oauthProvider"], publicClients: [ "auth", "oauthProvider", "publicClient" ], publicClient: (e, t) => t === void 0 ? [...a.publicClients, e ?? null] : [ ...a.publicClients, e ?? null, t ], consents: (t) => [ ...e.user(t), "oauthProvider", "consents" ], listConsents: (e, t) => [...a.consents(e), t ?? null], clients: (e) => [ "auth", "oauthProvider", "clients", e ?? null ] }; //#endregion //#region src/plugins/oauth-provider/delete-oauth-consent-mutation.ts function o(e, t) { return { mutationKey: i.deleteConsent, mutationFn: (t) => e.oauth2.deleteConsent({ ...t, fetchOptions: { ...t?.fetchOptions, throw: !0 } }), meta: { awaits: [a.consents(t)] } }; } //#endregion //#region src/plugins/oauth-provider/list-oauth-consents-query.ts function s(e, n, i) { return { queryKey: a.listConsents(n), queryFn: n ? ({ signal: n }) => e.oauth2.getConsents({ ...i, fetchOptions: t(i?.fetchOptions, n) }) : r }; } var c = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.ensureQueryData({ ...s(t, n, { fetchOptions: i }), ...a }); }, l = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.prefetchQuery({ ...s(t, n, { fetchOptions: i }), ...a }); }, u = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.fetchQuery({ ...s(t, n, { fetchOptions: i }), ...a }); }, d = (e, t, n) => e.getQueryData(a.listConsents(n)); //#endregion //#region src/plugins/oauth-provider/oauth-client-manager.ts function f(e) { let t = (e) => { if (e.type !== "user") throw Error("Better Auth's browser OAuth client endpoints cannot safely scope organization clients. Provide an organization OAuthClientManager instead."); }; return { async list(n, r) { return t(n), await e.oauth2.getClients({ fetchOptions: { signal: r, throw: !0 } }) ?? []; }, async create(n, r) { return t(n), e.oauth2.createClient({ ...r, fetchOptions: { throw: !0 } }); }, async update(n, r, i) { return t(n), e.oauth2.updateClient({ client_id: r, update: i, fetchOptions: { throw: !0 } }); }, async delete(n, r) { t(n), await e.oauth2.deleteClient({ client_id: r, fetchOptions: { throw: !0 } }); }, async rotateSecret(n, r) { return t(n), e.oauth2.client.rotateSecret({ client_id: r, fetchOptions: { throw: !0 } }); } }; } //#endregion //#region src/plugins/oauth-provider/oauth-client-mutations.ts var p = (e) => ({ awaits: [a.clients(e)] }); function m(e, t, n) { return { gcTime: 0, mutationKey: i.createClient, mutationFn: (n) => e.create(t, n), meta: p(n) }; } function h(e, t, n) { return { mutationKey: i.updateClient, mutationFn: ({ clientId: n, update: r }) => e.update(t, n, r), meta: p(n) }; } function g(e, t, n) { return { mutationKey: i.deleteClient, mutationFn: (n) => e.delete(t, n), meta: p(n) }; } function _(e, t, n) { return { gcTime: 0, mutationKey: i.rotateClientSecret, mutationFn: (n) => e.rotateSecret(t, n), meta: p(n) }; } function v(e, t, n) { return { mutationKey: i.setClientDisabled, mutationFn: ({ clientId: n, disabled: r }) => { if (!e.setDisabled) throw Error("This OAuth client manager does not support disabling clients."); return e.setDisabled(t, n, r); }, meta: p(n) }; } //#endregion //#region src/plugins/oauth-provider/oauth-clients-query.ts function y(e, t, n) { return { queryKey: a.clients(n), queryFn: n ? async ({ signal: n }) => (await e.list(t, n)).map(({ client_secret: e, ...t }) => t) : r }; } //#endregion //#region src/plugins/oauth-provider/oauth-consent-mutation.ts function b(e) { return { mutationKey: i.consent, mutationFn: (t) => e.oauth2.consent({ ...t, fetchOptions: { ...t?.fetchOptions, throw: !0 } }) }; } //#endregion //#region src/plugins/oauth-provider/oauth-consents.ts function x(e) { if (e == null) return; let t = e instanceof Date ? e : new Date(e); return Number.isNaN(t.getTime()) ? void 0 : t; } function S(e) { let t = /* @__PURE__ */ new Map(); for (let n of e ?? []) { if (!n?.clientId) continue; let e = t.get(n.clientId), r = e ?? { clientId: n.clientId, consentIds: [], scopes: [] }; r.consentIds.push(n.id); for (let e of n.scopes ?? []) e && !r.scopes.includes(e) && r.scopes.push(e); let i = x(n.updatedAt) ?? x(n.createdAt); i && (!r.updatedAt || i > r.updatedAt) && (r.updatedAt = i), e || t.set(n.clientId, r); } return Array.from(t.values()); } //#endregion //#region src/plugins/oauth-provider/oauth-continue-mutation.ts function C(e) { return { mutationKey: i.continue, mutationFn: (t) => e.oauth2.continue({ ...t, fetchOptions: { ...t.fetchOptions, throw: !0 } }) }; } //#endregion //#region src/plugins/oauth-provider/oauth-provider-localization.ts var w = { authorize: "Authorize {{client}}", authorizationDescription: "{{client}} wants to access your account.", authorizationRequest: "wants to access your account", redirectTo: "Authorizing will redirect you to", applicationInformation: "Application names and logos are provided by their developers. Only authorize applications you trust.", requestedPermissions: "This will allow {{client}} to:", signedInAs: "Signed in as", allow: "Authorize", cancel: "Cancel", privacyPolicy: "Privacy policy", termsOfService: "Terms of service", invalidRequest: "Invalid authorization request", invalidRequestDescription: "This authorization request is missing required information or is no longer valid.", application: "Application", selectAccount: "Choose an account", selectAccountDescription: "Pick the account you want to use with {{client}}.", currentAccount: "Current", continue: "Continue", noAccounts: "No accounts available", noAccountsDescription: "Sign in to continue to {{client}}.", accountCreated: "Account created", continuing: "Taking you back to {{client}}.", continueFailed: "Your account is ready, but we could not send you back to {{client}}.", tryAgain: "Try again", connectedApplications: "Connected applications", noConnectedApplications: "No connected applications", connectedApplicationsDescription: "Applications you authorize will show up here.", lastAuthorized: "Last authorized", removeAuthorization: "Remove authorization", removeAuthorizationTitle: "Remove authorization?", removeAuthorizationDescription: "This application will need your approval before receiving new access. Existing tokens may remain valid until they expire.", remove: "Remove", oauthClients: "OAuth clients", oauthClientsDescription: "Create and manage applications that can request access to accounts.", noOAuthClients: "No OAuth clients", noOAuthClientsDescription: "Create a client when your application is ready to use OAuth.", createClient: "Create client", editClient: "Edit client", clientName: "Client name", applicationType: "Application type", webApplication: "Web", nativeApplication: "Native", redirectUrls: "Redirect URLs", redirectUrlsDescription: "Enter one URL per line.", invalidUrl: "Enter a valid absolute URL.", applicationUrl: "Application URL", logoUrl: "Logo URL", permissions: "Permissions", scopes: "Scopes", saveChanges: "Save changes", clientId: "Client ID", clientSecret: "Client secret", clientSecretWarning: "Copy this secret now. It will not be shown again.", rotateSecret: "Rotate secret", rotateSecretTitle: "Rotate client secret?", rotateSecretDescription: "The current secret will stop working immediately.", deleteClient: "Delete client", deleteClientTitle: "Delete OAuth client?", deleteClientDescription: "This permanently removes the client and stops new authorization requests.", enabled: "Enabled", disabled: "Disabled", clientCreated: "Client created", secretRotated: "Secret rotated" }; //#endregion //#region src/plugins/oauth-provider/oauth-provider-plugin.ts function T(e) { if (e) try { let t = new URL(e); return t.protocol === "https:" || t.protocol === "http:" ? t.href : void 0; } catch { return; } } function E(e) { let t = new URLSearchParams(e).getAll("redirect_uri"); if (!(t.length !== 1 || !t[0])) try { let e = new URL(t[0]); return e.username || e.password ? void 0 : e.protocol === "https:" || e.protocol === "http:" ? e.origin : [ "javascript:", "data:", "file:", "blob:", "about:" ].includes(e.protocol) ? void 0 : e.host ? `${e.protocol}//${e.host}` : e.protocol; } catch { return; } } var D = (e) => Array.from(new Set((e ?? "").split(/\s+/).map((e) => e.trim()).filter(Boolean))); function O(e) { let t = new URLSearchParams(e); return { clientId: t.get("client_id")?.trim() || void 0, scopes: D(t.get("scope")), prompts: D(t.get("prompt")) }; } function k(e, t) { return e.prompts.includes(t); } var A = n("oauthProvider", (e = {}) => ({ localization: { ...w, ...e.localization }, scopeMetadata: e.scopeMetadata, showConnectedApplications: e.showConnectedApplications ?? !0, clientManagement: e.clientManagement ?? !!e.clientManager, clientManager: e.clientManager, organizationClientManager: e.organizationClientManager, viewPaths: { auth: { oauthConsent: e.path ?? "oauth-consent", oauthSignUp: e.signUpPath ?? "oauth-sign-up", oauthSelectAccount: e.selectAccountPath ?? "select-account" }, settings: { oauthClients: e.clientManagementPath ?? "oauth-clients" } } })), j = { openid: { label: "Verify your identity", description: "Access your unique account identifier." }, profile: { label: "View your profile", description: "View your name and profile picture." }, email: { label: "View your email address", description: "View your email address and whether it is verified." }, offline_access: { label: "Maintain access", description: "Access your data when you are not actively using the application." } }; function M(e, t) { return Object.hasOwn(e, t) ? e[t] : void 0; } function N(e, t, n) { return typeof e == "function" ? e(t, n) : Array.isArray(e) ? e.find((e) => e.scope === t) : M(e, t); } function P(e, t, n) { return (e ? N(e, t, n) : void 0) ?? M(j, t) ?? { label: t }; } //#endregion //#region src/plugins/oauth-provider/public-oauth-client-query.ts function F(e, n, i) { let { oauthQuery: o, ...s } = i ?? {}; return { queryKey: a.publicClient(n, o), queryFn: n ? ({ signal: r }) => { if (o !== void 0) { let e = new URLSearchParams(o).getAll("client_id"); if (e.length !== 1 || e[0] !== n) throw Error("OAuth client ID does not match the authorization request."); } let a = t(i?.fetchOptions, r); return o === void 0 ? e.oauth2.publicClient({ ...s, query: { client_id: n }, fetchOptions: a }) : e.oauth2.publicClientPrelogin({ client_id: n, oauth_query: o, fetchOptions: a }); } : r }; } var I = (e, t, n, r) => { let { fetchOptions: i, oauthQuery: a, ...o } = r ?? {}; return e.ensureQueryData({ ...F(t, n, { fetchOptions: i, oauthQuery: a }), ...o }); }, L = (e, t, n, r) => { let { fetchOptions: i, oauthQuery: a, ...o } = r ?? {}; return e.prefetchQuery({ ...F(t, n, { fetchOptions: i, oauthQuery: a }), ...o }); }, R = (e, t, n, r) => { let { fetchOptions: i, oauthQuery: a, ...o } = r ?? {}; return e.fetchQuery({ ...F(t, n, { fetchOptions: i, oauthQuery: a }), ...o }); }; //#endregion export { f as createBetterAuthOAuthClientManager, m as createOAuthClientOptions, g as deleteOAuthClientOptions, o as deleteOAuthConsentOptions, c as ensureListOAuthConsents, I as ensurePublicOAuthClient, u as fetchListOAuthConsents, R as fetchPublicOAuthClient, d as getListOAuthConsents, E as getOAuthAuthorizationDestination, S as groupOAuthConsents, k as hasOAuthPrompt, s as listOAuthConsentsOptions, y as oauthClientsOptions, b as oauthConsentOptions, C as oauthContinueOptions, w as oauthProviderLocalization, i as oauthProviderMutationKeys, A as oauthProviderPlugin, a as oauthProviderQueryKeys, j as oauthProviderScopeMetadata, O as parseOAuthAuthorizationRequest, l as prefetchListOAuthConsents, L as prefetchPublicOAuthClient, F as publicOAuthClientOptions, P as resolveOAuthScopeMetadata, _ as rotateOAuthClientSecretOptions, T as sanitizeOAuthClientUrl, v as setOAuthClientDisabledOptions, h as updateOAuthClientOptions };