Adds auth pages
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { InvitationValidation, InviteCompleteRequest } from '@/api/types';
|
||||
|
||||
interface ValidateState {
|
||||
data: InvitationValidation | null;
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
export function useValidateInvitation(token: string | undefined): ValidateState {
|
||||
const [state, setState] = useState<ValidateState>({ data: null, isLoading: token !== undefined, error: null });
|
||||
const mounted = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
mounted.current = true;
|
||||
return () => {
|
||||
mounted.current = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (token === undefined) {
|
||||
setState({ data: null, isLoading: false, error: null });
|
||||
return;
|
||||
}
|
||||
|
||||
setState({ data: null, isLoading: true, error: null });
|
||||
|
||||
api.get<InvitationValidation>(`/Invitation/validate?token=${encodeURIComponent(token)}`)
|
||||
.then((data) => {
|
||||
if (mounted.current) setState({ data, isLoading: false, error: null });
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (mounted.current)
|
||||
setState({
|
||||
data: null,
|
||||
isLoading: false,
|
||||
error: err instanceof Error ? err : new Error(String(err)),
|
||||
});
|
||||
});
|
||||
}, [token]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
interface CompleteSetupState {
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
interface UseCompleteSetup extends CompleteSetupState {
|
||||
mutate: (data: InviteCompleteRequest) => Promise<void>;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
export function useCompleteSetup(): UseCompleteSetup {
|
||||
const [state, setState] = useState<CompleteSetupState>({ isLoading: false, error: null });
|
||||
|
||||
const mutate = useCallback(async (data: InviteCompleteRequest): Promise<void> => {
|
||||
setState({ isLoading: true, error: null });
|
||||
try {
|
||||
await api.post('/Invitation/complete', data);
|
||||
setState({ isLoading: false, error: null });
|
||||
} catch (err: unknown) {
|
||||
setState({ isLoading: false, error: err instanceof Error ? err : new Error(String(err)) });
|
||||
throw err;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const reset = useCallback(() => setState({ isLoading: false, error: null }), []);
|
||||
|
||||
return { ...state, mutate, reset };
|
||||
}
|
||||
Reference in New Issue
Block a user