mirror of
https://github.com/better-auth/better-auth.git
synced 2026-06-05 05:46:50 -05:00
fix: renmae leading _ to $ for signals
This commit is contained in:
@@ -27,12 +27,12 @@ export const getClientConfig = <O extends ClientOptions>(options?: O) => {
|
||||
...pluginsFetchPlugins,
|
||||
],
|
||||
});
|
||||
const { _sessionSignal, $session } = getSessionAtom<O>($fetch);
|
||||
const { $sessionSignal, session } = getSessionAtom<O>($fetch);
|
||||
const plugins = options?.plugins || [];
|
||||
let pluginsActions = {} as Record<string, any>;
|
||||
let pluginsAtoms = {
|
||||
_sessionSignal,
|
||||
session: $session,
|
||||
$sessionSignal,
|
||||
session,
|
||||
} as Record<string, WritableAtom<any>>;
|
||||
let pluginPathMethods: Record<string, "POST" | "GET"> = {
|
||||
"/sign-out": "POST",
|
||||
@@ -40,7 +40,7 @@ export const getClientConfig = <O extends ClientOptions>(options?: O) => {
|
||||
};
|
||||
const atomListeners: AtomListener[] = [
|
||||
{
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal",
|
||||
matcher(path) {
|
||||
return (
|
||||
path === "/sign-out" ||
|
||||
@@ -65,13 +65,13 @@ export const getClientConfig = <O extends ClientOptions>(options?: O) => {
|
||||
}
|
||||
|
||||
const $store = {
|
||||
notify: (signal?: Omit<string, "_sessionSignal"> | "_sessionSignal") => {
|
||||
notify: (signal?: Omit<string, "$sessionSignal"> | "$sessionSignal") => {
|
||||
pluginsAtoms[signal as keyof typeof pluginsAtoms].set(
|
||||
!pluginsAtoms[signal as keyof typeof pluginsAtoms].get(),
|
||||
);
|
||||
},
|
||||
listen: (
|
||||
signal: Omit<string, "_sessionSignal"> | "_sessionSignal",
|
||||
signal: Omit<string, "$sessionSignal"> | "$sessionSignal",
|
||||
listener: (value: boolean, oldValue?: boolean | undefined) => void,
|
||||
) => {
|
||||
pluginsAtoms[signal as keyof typeof pluginsAtoms].subscribe(listener);
|
||||
|
||||
@@ -40,6 +40,7 @@ export const useAuthQuery = <T>(
|
||||
isPending: value.get().isPending,
|
||||
})
|
||||
: options;
|
||||
|
||||
return $fetch<T>(path, {
|
||||
...opts,
|
||||
onSuccess: async (context) => {
|
||||
|
||||
@@ -21,7 +21,7 @@ export function getSessionAtom<Option extends ClientOptions>(
|
||||
method: "GET",
|
||||
});
|
||||
return {
|
||||
$session: session,
|
||||
_sessionSignal: $signal,
|
||||
session,
|
||||
$sessionSignal: $signal,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ const serverPlugin = {
|
||||
} satisfies BetterAuthPlugin;
|
||||
|
||||
export const testClientPlugin = () => {
|
||||
const _test = atom(false);
|
||||
const $test = atom(false);
|
||||
let testValue = 0;
|
||||
const computedAtom = computed(_test, () => {
|
||||
const computedAtom = computed($test, () => {
|
||||
return testValue++;
|
||||
});
|
||||
return {
|
||||
@@ -63,7 +63,7 @@ export const testClientPlugin = () => {
|
||||
getActions($fetch) {
|
||||
return {
|
||||
setTestAtom(value: boolean) {
|
||||
_test.set(value);
|
||||
$test.set(value);
|
||||
},
|
||||
test: {
|
||||
signOut: async () => {},
|
||||
@@ -71,13 +71,13 @@ export const testClientPlugin = () => {
|
||||
};
|
||||
},
|
||||
getAtoms($fetch) {
|
||||
const _signal = atom(false);
|
||||
const queryAtom = useAuthQuery<any>(_signal, "/test", $fetch, {
|
||||
const $signal = atom(false);
|
||||
const queryAtom = useAuthQuery<any>($signal, "/test", $fetch, {
|
||||
method: "GET",
|
||||
});
|
||||
return {
|
||||
_test,
|
||||
_signal,
|
||||
$test,
|
||||
$signal,
|
||||
computedAtom,
|
||||
queryAtom,
|
||||
};
|
||||
@@ -86,37 +86,37 @@ export const testClientPlugin = () => {
|
||||
atomListeners: [
|
||||
{
|
||||
matcher: (path) => path === "/test",
|
||||
signal: "_test",
|
||||
signal: "$test",
|
||||
},
|
||||
{
|
||||
matcher: (path) => path === "/test2/sign-out",
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal",
|
||||
},
|
||||
],
|
||||
} satisfies BetterAuthClientPlugin;
|
||||
};
|
||||
export const testClientPlugin2 = () => {
|
||||
const _test2 = atom(false);
|
||||
const $test2 = atom(false);
|
||||
let testValue = 0;
|
||||
const anotherAtom = computed(_test2, () => {
|
||||
const anotherAtom = computed($test2, () => {
|
||||
return testValue++;
|
||||
});
|
||||
return {
|
||||
id: "test",
|
||||
getAtoms($fetch) {
|
||||
return {
|
||||
_test2,
|
||||
$test2,
|
||||
anotherAtom,
|
||||
};
|
||||
},
|
||||
atomListeners: [
|
||||
{
|
||||
matcher: (path) => path === "/test",
|
||||
signal: "_test",
|
||||
signal: "$test",
|
||||
},
|
||||
{
|
||||
matcher: (path) => path === "/test2/sign-out",
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal",
|
||||
},
|
||||
],
|
||||
} satisfies BetterAuthClientPlugin;
|
||||
|
||||
@@ -17,7 +17,7 @@ import type { InferFieldsInputClient, InferFieldsOutput } from "../db";
|
||||
|
||||
export type AtomListener = {
|
||||
matcher: (path: string) => boolean;
|
||||
signal: "_sessionSignal" | Omit<string, "_sessionSignal">;
|
||||
signal: "$sessionSignal" | Omit<string, "$sessionSignal">;
|
||||
};
|
||||
|
||||
interface Store {
|
||||
@@ -99,9 +99,9 @@ export type InferActions<O extends ClientOptions> = O["plugins"] extends Array<
|
||||
: {};
|
||||
/**
|
||||
* signals are just used to recall a computed value.
|
||||
* as a convention they start with "_"
|
||||
* as a convention they start with "$"
|
||||
*/
|
||||
export type IsSignal<T> = T extends `_${infer _}` ? true : false;
|
||||
export type IsSignal<T> = T extends `$${infer _}` ? true : false;
|
||||
|
||||
export type InferPluginsFromClient<O extends ClientOptions> =
|
||||
O["plugins"] extends Array<BetterAuthClientPlugin>
|
||||
|
||||
@@ -83,7 +83,7 @@ export function createAuthClient<Option extends ClientOptions>(
|
||||
useFetch?: UseFetch,
|
||||
) {
|
||||
if (useFetch) {
|
||||
const ref = useStore(pluginsAtoms._sessionSignal);
|
||||
const ref = useStore(pluginsAtoms.$sessionSignal);
|
||||
const baseURL = options?.fetchOptions?.baseURL || options?.baseURL;
|
||||
const authPath = baseURL ? new URL(baseURL).pathname : "/api/auth";
|
||||
return useFetch(`${authPath}/get-session`, {
|
||||
|
||||
@@ -13,7 +13,7 @@ export const multiSessionClient = () => {
|
||||
matcher(path) {
|
||||
return path === "/multi-session/set-active";
|
||||
},
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal"
|
||||
},
|
||||
],
|
||||
} satisfies BetterAuthClientPlugin;
|
||||
|
||||
@@ -22,8 +22,8 @@ export const organizationClient = <O extends OrganizationClientOptions>(
|
||||
options?: O,
|
||||
) => {
|
||||
const activeOrgId = atom<string | null | undefined>(undefined);
|
||||
const _listOrg = atom<boolean>(false);
|
||||
const _activeOrgSignal = atom<boolean>(false);
|
||||
const $listOrg = atom<boolean>(false);
|
||||
const $activeOrgSignal = atom<boolean>(false);
|
||||
|
||||
type DefaultStatements = typeof defaultStatements;
|
||||
type Statements = O["ac"] extends AccessControl<infer S>
|
||||
@@ -80,7 +80,7 @@ export const organizationClient = <O extends OrganizationClientOptions>(
|
||||
}),
|
||||
getAtoms: ($fetch) => {
|
||||
const listOrganizations = useAuthQuery<Organization[]>(
|
||||
_listOrg,
|
||||
$listOrg,
|
||||
"/organization/list",
|
||||
$fetch,
|
||||
{
|
||||
@@ -102,7 +102,7 @@ export const organizationClient = <O extends OrganizationClientOptions>(
|
||||
}
|
||||
>
|
||||
>(
|
||||
[activeOrgId, _activeOrgSignal],
|
||||
[activeOrgId, $activeOrgSignal],
|
||||
"/organization/activate",
|
||||
$fetch,
|
||||
() => ({
|
||||
@@ -114,8 +114,8 @@ export const organizationClient = <O extends OrganizationClientOptions>(
|
||||
);
|
||||
|
||||
return {
|
||||
_listOrg,
|
||||
_activeOrgSignal,
|
||||
$listOrg,
|
||||
$activeOrgSignal,
|
||||
activeOrganization,
|
||||
listOrganizations,
|
||||
};
|
||||
@@ -127,13 +127,13 @@ export const organizationClient = <O extends OrganizationClientOptions>(
|
||||
path === "/organization/create" || path === "/organization/delete"
|
||||
);
|
||||
},
|
||||
signal: "_listOrg",
|
||||
signal: "$listOrg",
|
||||
},
|
||||
{
|
||||
matcher(path) {
|
||||
return path.startsWith("/organization");
|
||||
},
|
||||
signal: "_activeOrgSignal",
|
||||
signal: "$activeOrgSignal",
|
||||
},
|
||||
],
|
||||
} satisfies BetterAuthClientPlugin;
|
||||
|
||||
@@ -12,7 +12,7 @@ export const phoneNumberClient = () => {
|
||||
path === "/phone-number/update" || path === "/phone-number/verify"
|
||||
);
|
||||
},
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal"
|
||||
},
|
||||
],
|
||||
} satisfies BetterAuthClientPlugin;
|
||||
|
||||
@@ -21,7 +21,7 @@ export const twoFactorClient = (
|
||||
atomListeners: [
|
||||
{
|
||||
matcher: (path) => path.startsWith("/two-factor/"),
|
||||
signal: "_sessionSignal",
|
||||
signal: "$sessionSignal"
|
||||
},
|
||||
],
|
||||
pathMethods: {
|
||||
|
||||
Reference in New Issue
Block a user