UNPKG

@better-auth-ui/core

Version:

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

417 lines (416 loc) 11.9 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) => [...a.publicClients, e ?? null], 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 { 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 { 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 ? ({ signal: n }) => e.list(t, n) : 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.", requestedPermissions: "This will allow {{client}} to:", signedInAs: "Signed in as", allow: "Allow", 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", 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; } } var E = (e) => Array.from(new Set((e ?? "").split(/\s+/).map((e) => e.trim()).filter(Boolean))); function D(e) { let t = new URLSearchParams(e); return { clientId: t.get("client_id")?.trim() || void 0, scopes: E(t.get("scope")), prompts: E(t.get("prompt")) }; } function O(e, t) { return e.prompts.includes(t); } var k = 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" } } })), A = { 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 j(e, t) { return Object.hasOwn(e, t) ? e[t] : void 0; } function M(e, t, n) { return typeof e == "function" ? e(t, n) : Array.isArray(e) ? e.find((e) => e.scope === t) : j(e, t); } function N(e, t, n) { return (e ? M(e, t, n) : void 0) ?? j(A, t) ?? { label: t }; } //#endregion //#region src/plugins/oauth-provider/public-oauth-client-query.ts function P(e, n, i) { return { queryKey: a.publicClient(n), queryFn: n ? ({ signal: r }) => e.oauth2.publicClient({ ...i, query: { client_id: n }, fetchOptions: t(i?.fetchOptions, r) }) : r }; } var F = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.ensureQueryData({ ...P(t, n, { fetchOptions: i }), ...a }); }, I = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.prefetchQuery({ ...P(t, n, { fetchOptions: i }), ...a }); }, L = (e, t, n, r) => { let { fetchOptions: i, ...a } = r ?? {}; return e.fetchQuery({ ...P(t, n, { fetchOptions: i }), ...a }); }; //#endregion export { f as createBetterAuthOAuthClientManager, m as createOAuthClientOptions, g as deleteOAuthClientOptions, o as deleteOAuthConsentOptions, c as ensureListOAuthConsents, F as ensurePublicOAuthClient, u as fetchListOAuthConsents, L as fetchPublicOAuthClient, d as getListOAuthConsents, S as groupOAuthConsents, O as hasOAuthPrompt, s as listOAuthConsentsOptions, y as oauthClientsOptions, b as oauthConsentOptions, C as oauthContinueOptions, w as oauthProviderLocalization, i as oauthProviderMutationKeys, k as oauthProviderPlugin, a as oauthProviderQueryKeys, A as oauthProviderScopeMetadata, D as parseOAuthAuthorizationRequest, l as prefetchListOAuthConsents, I as prefetchPublicOAuthClient, P as publicOAuthClientOptions, N as resolveOAuthScopeMetadata, _ as rotateOAuthClientSecretOptions, T as sanitizeOAuthClientUrl, v as setOAuthClientDisabledOptions, h as updateOAuthClientOptions };