UNPKG

@adonis-agora/authkit-react

Version:

Frontend ergonomics over AuthKit for AdonisJS + Inertia + React apps: a typed useAuth() hook, role-gating hooks and gating components.

33 lines (32 loc) 1.12 kB
import { useCallback, useState } from 'react'; import { useAuthkitConfig } from '../config.js'; import { useAuth } from '../use_auth.js'; import { jsonRequest } from './use_resource.js'; export function useProfile() { const config = useAuthkitConfig(); const { user } = useAuth(); const [state, setState] = useState({ data: user, loading: false, error: null, }); const update = useCallback(async (data) => { setState((s) => ({ ...s, loading: true, error: null })); try { const updated = await jsonRequest(config.endpoints.profile, { method: 'POST', body: JSON.stringify(data), csrfToken: config.csrfToken, }); setState({ data: updated ?? { ...user, ...data }, loading: false, error: null, }); } catch (err) { setState((s) => ({ ...s, loading: false, error: err })); } }, [config.endpoints.profile, config.csrfToken, user]); return { ...state, actions: { update } }; }