@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.
@@ -1,24 +1,25 @@
1
1
  "use client";
2
2
  import { useCallback } from "react";
3
3
  function useSignOut({
4
- postLogoutRedirectPath = "/auth/sign-in?logout=success",
5
- retrieveLogoutUrlPath = "/api/auth/sign-out-url",
6
- onLogoutSuccess,
4
+ postSignOutRedirectPath = "/auth/sign-in?sign-out=success",
5
+ retrieveSignOutUrlPath = "/api/auth/sign-out-url",
6
+ onSignOutSuccess,
7
7
  authClient,
8
- origin = window.location.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: onLogoutSuccess
14
+ onSuccess: onSignOutSuccess
14
15
  }
15
16
  });
16
- }, [authClient, onLogoutSuccess]);
17
+ }, [authClient, onSignOutSuccess]);
17
18
  const handleSignOut = useCallback(async () => {
18
- const postLogoutRedirectUri = `${origin}${postLogoutRedirectPath}`;
19
+ const postLogoutRedirectUri = `${origin}${postSignOutRedirectPath}`;
19
20
  try {
20
21
  const response = await fetch(
21
- `${retrieveLogoutUrlPath}?post_logout_redirect_uri=${encodeURIComponent(postLogoutRedirectUri)}`
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
- postLogoutRedirectPath,
45
- retrieveLogoutUrlPath,
48
+ postSignOutRedirectPath,
49
+ retrieveSignOutUrlPath,
46
50
  standardSignOut
47
51
  ]);
48
52
  return { handleSignOut };
@@ -1,22 +1,21 @@
1
- import type { AuthClient } from "better-auth/client";
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.postLogoutRedirectPath - Path to redirect to after logout (defaults to "/auth/sign-in")
8
- * @returns Object containing handleSignOut function to initiate the logout process
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({ postLogoutRedirectPath: "/auth/sign-in" });
11
+ * const { handleSignOut } = useSignOut({ postSignOutRedirectPath: "/auth/sign-in?sign-out=success" });
13
12
  * ```
14
13
  */
15
- export declare function useSignOut({ postLogoutRedirectPath, retrieveLogoutUrlPath, onLogoutSuccess, authClient, origin, }: {
16
- postLogoutRedirectPath?: string;
17
- retrieveLogoutUrlPath?: string;
18
- onLogoutSuccess?: () => void;
19
- authClient: AuthClient<ReturnType<typeof getBetterAuthClientOptions>>;
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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imtf/auth",
3
- "version": "0.0.1-alpha.1",
3
+ "version": "0.0.1-alpha.3",
4
4
  "description": "Better auth wrapper for imtf usage",
5
5
  "license": "UNLICENSED",
6
6
  "author": "",