@better-auth-ui/react 1.6.28 → 1.6.29
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/dist/hooks/auth/use-last-login-method.d.ts +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +262 -249
- package/dist/lib/auth-client.d.ts +4 -1
- package/package.json +1 -1
- package/src/hooks/auth/use-last-login-method.ts +51 -0
- package/src/index.ts +1 -0
- package/src/lib/auth-client.ts +7 -0
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { apiKeyClient } from '@better-auth/api-key/client';
|
|
2
2
|
import { passkeyClient } from '@better-auth/passkey/client';
|
|
3
|
-
import { magicLinkClient, multiSessionClient, organizationClient, usernameClient } from 'better-auth/client/plugins';
|
|
3
|
+
import { lastLoginMethodClient, magicLinkClient, multiSessionClient, organizationClient, usernameClient } from 'better-auth/client/plugins';
|
|
4
4
|
import { createAuthClient } from 'better-auth/react';
|
|
5
5
|
export type AuthClient = ReturnType<typeof createAuthClient>;
|
|
6
6
|
export type MagicLinkAuthClient = ReturnType<typeof createAuthClient<{
|
|
7
7
|
plugins: [ReturnType<typeof magicLinkClient>];
|
|
8
8
|
}>>;
|
|
9
|
+
export type LastLoginMethodAuthClient = ReturnType<typeof createAuthClient<{
|
|
10
|
+
plugins: [ReturnType<typeof lastLoginMethodClient>];
|
|
11
|
+
}>>;
|
|
9
12
|
export type MultiSessionAuthClient = ReturnType<typeof createAuthClient<{
|
|
10
13
|
plugins: [ReturnType<typeof multiSessionClient>];
|
|
11
14
|
}>>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type LastLoginMethodLocalization,
|
|
5
|
+
lastLoginMethodLocalization,
|
|
6
|
+
lastLoginMethodPlugin
|
|
7
|
+
} from "@better-auth-ui/core/plugins"
|
|
8
|
+
import { useCallback, useSyncExternalStore } from "react"
|
|
9
|
+
import { useAuth } from "../../components/auth/auth-provider"
|
|
10
|
+
import type { LastLoginMethodAuthClient } from "../../lib/auth-client"
|
|
11
|
+
|
|
12
|
+
const subscribe = () => () => {}
|
|
13
|
+
const getServerSnapshot = () => null
|
|
14
|
+
|
|
15
|
+
export type LastLoginMethodState = {
|
|
16
|
+
/** Most recently used method, such as `"email"`, `"google"`, or `"github"`. */
|
|
17
|
+
method: string | null
|
|
18
|
+
/** Localization contributed by `lastLoginMethodPlugin()`. */
|
|
19
|
+
localization: LastLoginMethodLocalization
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reads Better Auth's last-login-method cookie after hydration.
|
|
24
|
+
*
|
|
25
|
+
* Always returns an object containing `method` and `localization`. Only
|
|
26
|
+
* `method` is `null` when the UI plugin is not registered or the matching
|
|
27
|
+
* Better Auth client plugin has no stored method.
|
|
28
|
+
*/
|
|
29
|
+
export function useLastLoginMethod(): LastLoginMethodState {
|
|
30
|
+
const { authClient, plugins } = useAuth()
|
|
31
|
+
const plugin = plugins.find(
|
|
32
|
+
(candidate) => candidate.id === lastLoginMethodPlugin.id
|
|
33
|
+
)
|
|
34
|
+
const localization = {
|
|
35
|
+
...lastLoginMethodLocalization,
|
|
36
|
+
...(plugin?.localization as
|
|
37
|
+
| Partial<LastLoginMethodLocalization>
|
|
38
|
+
| undefined)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const getSnapshot = useCallback(() => {
|
|
42
|
+
if (!plugin) return null
|
|
43
|
+
|
|
44
|
+
const client = authClient as Partial<LastLoginMethodAuthClient>
|
|
45
|
+
return client.getLastUsedLoginMethod?.() ?? null
|
|
46
|
+
}, [authClient, plugin])
|
|
47
|
+
|
|
48
|
+
const method = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
|
|
49
|
+
|
|
50
|
+
return { method, localization }
|
|
51
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./components/auth/fetch-options-provider"
|
|
|
5
5
|
export * from "./components/icons"
|
|
6
6
|
export * from "./components/settings/account/theme-preview"
|
|
7
7
|
export * from "./hooks/auth/use-authenticate"
|
|
8
|
+
export * from "./hooks/auth/use-last-login-method"
|
|
8
9
|
export * from "./hooks/auth/use-user"
|
|
9
10
|
export * from "./hooks/use-auth-mutation"
|
|
10
11
|
export * from "./hooks/use-auth-plugin"
|
package/src/lib/auth-client.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { apiKeyClient } from "@better-auth/api-key/client"
|
|
2
2
|
import type { passkeyClient } from "@better-auth/passkey/client"
|
|
3
3
|
import type {
|
|
4
|
+
lastLoginMethodClient,
|
|
4
5
|
magicLinkClient,
|
|
5
6
|
multiSessionClient,
|
|
6
7
|
organizationClient,
|
|
@@ -20,6 +21,12 @@ export type MagicLinkAuthClient = ReturnType<
|
|
|
20
21
|
typeof createAuthClient<{ plugins: [ReturnType<typeof magicLinkClient>] }>
|
|
21
22
|
>
|
|
22
23
|
|
|
24
|
+
export type LastLoginMethodAuthClient = ReturnType<
|
|
25
|
+
typeof createAuthClient<{
|
|
26
|
+
plugins: [ReturnType<typeof lastLoginMethodClient>]
|
|
27
|
+
}>
|
|
28
|
+
>
|
|
29
|
+
|
|
23
30
|
export type MultiSessionAuthClient = ReturnType<
|
|
24
31
|
typeof createAuthClient<{ plugins: [ReturnType<typeof multiSessionClient>] }>
|
|
25
32
|
>
|