Files
webmail/hooks/use-stalwart-admin.ts
T
lucamzanon 4347d25890 fix(mountain-view): show the admin console link in the top bar
The top bar replaces the navigation rail on desktop, and with it went
the shield that takes a Stalwart admin to the console, so an admin under
Mountain View had no way in. The bar now shows it, with the same update
dot. The check that decides it moves out of the rail into
useStalwartAdmin so both use the same one.
2026-10-09 08:28:40 +02:00

40 lines
1.4 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { getActiveAccountSlotHeaders } from "@/lib/auth/active-account-slot";
import { apiFetch } from "@/lib/browser-navigation";
import { IS_LITE } from "@/lib/lite";
/**
* Whether the signed-in user may open the Stalwart admin console. In "auto"
* mode the admin session is minted here as well, so /admin works even after a
* full page navigation; in "password" mode the console asks for it (#870).
*/
export function useStalwartAdmin(): boolean {
const [isStalwartAdmin, setIsStalwartAdmin] = useState(false);
useEffect(() => {
let cancelled = false;
const headers = getActiveAccountSlotHeaders();
// No admin console in the static Lite build.
if (IS_LITE || !headers['X-JMAP-Cookie-Slot']) return;
apiFetch('/api/admin/auth', { headers })
.then(res => res.json())
.then(data => {
if (cancelled || !data.stalwartAdmin) return;
setIsStalwartAdmin(true);
if (!data.authenticated && data.stalwartAutoLogin === true) {
apiFetch('/api/admin/auth', {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...headers },
body: JSON.stringify({ stalwartAuth: true }),
}).catch(() => {});
}
})
.catch(() => {});
return () => { cancelled = true; };
}, []);
return isStalwartAdmin;
}