@imtf/auth 0.0.1-alpha.1 → 0.0.1-alpha.3
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/lib/hooks/useSignOut.js +14 -10
- package/lib/types/hooks/useSignOut.d.ts +9 -10
- package/package.json +1 -1
package/lib/hooks/useSignOut.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useCallback } from "react";
|
|
3
3
|
function useSignOut({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
postSignOutRedirectPath = "/auth/sign-in?sign-out=success",
|
|
5
|
+
retrieveSignOutUrlPath = "/api/auth/sign-out-url",
|
|
6
|
+
onSignOutSuccess,
|
|
7
7
|
authClient,
|
|
8
|
-
origin
|
|
8
|
+
origin: providedOrigin
|
|
9
9
|
}) {
|
|
10
|
+
const origin = providedOrigin ?? (typeof window !== "undefined" ? window.location.origin : "");
|
|
10
11
|
const standardSignOut = useCallback(() => {
|
|
11
12
|
void authClient.signOut({
|
|
12
13
|
fetchOptions: {
|
|
13
|
-
onSuccess:
|
|
14
|
+
onSuccess: onSignOutSuccess
|
|
14
15
|
}
|
|
15
16
|
});
|
|
16
|
-
}, [authClient,
|
|
17
|
+
}, [authClient, onSignOutSuccess]);
|
|
17
18
|
const handleSignOut = useCallback(async () => {
|
|
18
|
-
const postLogoutRedirectUri = `${origin}${
|
|
19
|
+
const postLogoutRedirectUri = `${origin}${postSignOutRedirectPath}`;
|
|
19
20
|
try {
|
|
20
21
|
const response = await fetch(
|
|
21
|
-
`${
|
|
22
|
+
`${retrieveSignOutUrlPath}?post_logout_redirect_uri=${encodeURIComponent(postLogoutRedirectUri)}`
|
|
22
23
|
);
|
|
23
24
|
if (!response.ok) {
|
|
24
25
|
const { error } = await response.json().catch(() => ({ error: null }));
|
|
@@ -30,6 +31,9 @@ function useSignOut({
|
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
await authClient.signOut();
|
|
34
|
+
if (typeof window === "undefined") {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
33
37
|
window.location.href = url;
|
|
34
38
|
} catch (error) {
|
|
35
39
|
console.error(
|
|
@@ -41,8 +45,8 @@ function useSignOut({
|
|
|
41
45
|
}, [
|
|
42
46
|
authClient,
|
|
43
47
|
origin,
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
postSignOutRedirectPath,
|
|
49
|
+
retrieveSignOutUrlPath,
|
|
46
50
|
standardSignOut
|
|
47
51
|
]);
|
|
48
52
|
return { handleSignOut };
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { getBetterAuthClientOptions } from "../index";
|
|
1
|
+
import type { createAuthClient } from "better-auth/react";
|
|
3
2
|
/**
|
|
4
3
|
* Custom hook for handling user sign-out with support for federated logout.
|
|
5
4
|
*
|
|
6
5
|
* @param params - Configuration options
|
|
7
|
-
* @param params.
|
|
8
|
-
* @returns Object containing handleSignOut function to initiate the
|
|
6
|
+
* @param params.postSignOutRedirectPath - Path to redirect to after sign-out (defaults to "/auth/sign-in?sign-out=success")
|
|
7
|
+
* @returns Object containing handleSignOut function to initiate the sign-out process
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* ```tsx
|
|
12
|
-
* const { handleSignOut } = useSignOut({
|
|
11
|
+
* const { handleSignOut } = useSignOut({ postSignOutRedirectPath: "/auth/sign-in?sign-out=success" });
|
|
13
12
|
* ```
|
|
14
13
|
*/
|
|
15
|
-
export declare function useSignOut({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
authClient:
|
|
14
|
+
export declare function useSignOut({ postSignOutRedirectPath, retrieveSignOutUrlPath, onSignOutSuccess, authClient, origin: providedOrigin, }: {
|
|
15
|
+
postSignOutRedirectPath?: string;
|
|
16
|
+
retrieveSignOutUrlPath?: string;
|
|
17
|
+
onSignOutSuccess?: () => void;
|
|
18
|
+
authClient: ReturnType<typeof createAuthClient>;
|
|
20
19
|
origin?: string;
|
|
21
20
|
}): {
|
|
22
21
|
handleSignOut: () => Promise<void>;
|