mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-29 19:41:32 -05:00
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { signOut } from "next-auth/react";
|
|
import { logger } from "@formbricks/logger";
|
|
import { FORMBRICKS_ENVIRONMENT_ID_LS } from "@/lib/localStorage";
|
|
import { logSignOutAction } from "@/modules/auth/actions/sign-out";
|
|
|
|
interface UseSignOutOptions {
|
|
reason?:
|
|
| "user_initiated"
|
|
| "account_deletion"
|
|
| "email_change"
|
|
| "session_timeout"
|
|
| "forced_logout"
|
|
| "password_reset";
|
|
redirectUrl?: string;
|
|
organizationId?: string;
|
|
redirect?: boolean;
|
|
callbackUrl?: string;
|
|
clearEnvironmentId?: boolean;
|
|
}
|
|
|
|
interface SessionUser {
|
|
id: string;
|
|
email?: string;
|
|
}
|
|
|
|
/**
|
|
* Custom hook to handle sign out with audit logging
|
|
* @param sessionUser - The current user session data (optional)
|
|
* @returns {Object} - An object containing the signOutWithAudit function
|
|
*/
|
|
export const useSignOut = (sessionUser?: SessionUser | null) => {
|
|
const signOutWithAudit = async (options?: UseSignOutOptions) => {
|
|
// Log audit event before signing out (server action)
|
|
if (sessionUser?.id) {
|
|
try {
|
|
await logSignOutAction(sessionUser.id, sessionUser.email ?? "", {
|
|
reason: options?.reason || "user_initiated", // NOSONAR // We want to check for empty strings
|
|
redirectUrl: options?.redirectUrl || options?.callbackUrl, // NOSONAR // We want to check for empty strings
|
|
organizationId: options?.organizationId,
|
|
});
|
|
} catch (error) {
|
|
// Don't block signOut if audit logging fails
|
|
logger.error("Failed to log signOut event:", error);
|
|
}
|
|
}
|
|
|
|
if (options?.clearEnvironmentId) {
|
|
localStorage.removeItem(FORMBRICKS_ENVIRONMENT_ID_LS);
|
|
}
|
|
|
|
// Call NextAuth signOut
|
|
return await signOut({
|
|
redirect: options?.redirect,
|
|
callbackUrl: options?.callbackUrl,
|
|
});
|
|
};
|
|
|
|
return { signOut: signOutWithAudit };
|
|
};
|