Public Access
feat(admin): add the admin users page and view as
This commit is contained in:
1 parent
46587dfb5c
commit
afe00ceae9
14 files changed
+649
-8
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import {useEffect, useState} from 'react';
|
||||
import {apiFetch} from '../utils/api.js';
|
||||
import {sessionStamp} from '../utils/auth.js';
|
||||
|
||||
/**
|
||||
* The stored role and the active role ("view as") of the logged-in user, or null while loading and on any error.
|
||||
* Loads again after a new login or a role switch (see sessionStamp) on the next render.
|
||||
*/
|
||||
export default function useCurrentUser(enabled) {
|
||||
const [user, setUser] = useState(null);
|
||||
const stamp = sessionStamp();
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !stamp) {
|
||||
setUser(null);
|
||||
return undefined;
|
||||
}
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
try {
|
||||
const response = await apiFetch(`${import.meta.env.VITE_API_URL}/api/users/me?v=1`);
|
||||
const data = response.ok ? await response.json() : null;
|
||||
if (!cancelled) setUser(data && {role: data.role, activeRole: data.activeRole ?? data.role});
|
||||
} catch {
|
||||
if (!cancelled) setUser(null);
|
||||
}
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [enabled, stamp]);
|
||||
|
||||
return user;
|
||||
}
|
||||
Reference in new issue
Block a user