Files
TREK/client/src/components/Admin/useAdminUserActions.ts
T
Maurice 6c5ab5414a refactor(admin): share the JWT rotation and passkey reset between desktop and phone
useAdminUserActions now rotates the JWT secret and resets a user's passkeys for the desktop dialogs and the phone sheets. Each shell keeps its own confirm step, and the phone sheet still stays busy until the reset settles.
2026-10-09 15:19:47 +02:00

47 lines
1.6 KiB
TypeScript

import { adminApi } from '../../api/client';
import type { TranslationFn } from '../../types';
interface UserActionsHost {
toast: { success: (message: string) => void; error: (message: string) => void };
setRotatingJwt: (value: boolean) => void;
setShowRotateJwtModal: (value: boolean) => void;
logout: () => void;
navigate: (to: string, options?: { state?: unknown }) => void;
}
/**
* The two irreversible user actions behind the admin dialogs and their phone sheets:
* rotating the JWT secret, which signs everyone out including the admin, and removing a
* user's passkeys. Each shell keeps its own confirm step and busy state around them.
*/
export function useAdminUserActions(admin: UserActionsHost, t: TranslationFn) {
const { toast, setRotatingJwt, setShowRotateJwtModal, logout, navigate } = admin;
const rotateJwt = async () => {
setRotatingJwt(true);
try {
await adminApi.rotateJwtSecret();
setShowRotateJwtModal(false);
logout();
navigate('/login', { state: { noRedirect: true } });
} catch {
toast.error(t('common.error'));
setRotatingJwt(false);
}
};
/** Removes the user's passkeys and toasts how many went; true when it worked. */
const resetPasskeys = async (user: { id: number }): Promise<boolean> => {
try {
const r = await adminApi.resetUserPasskeys(user.id);
toast.success(t('admin.passkey.resetDone', { count: r.deleted ?? 0 }));
return true;
} catch {
toast.error(t('common.error'));
return false;
}
};
return { rotateJwt, resetPasskeys };
}