import { AxiosError } from 'axios' ; import { NextRouter } from 'next/router' ; import { Dispatch , SetStateAction } from 'react' ; import { toast } from 'react-toastify' ; export function handleGetFlowError < S > ( router , flowType , resetFlow ) { return async ( err ) => { switch ( err . response ?. data . error ?. id ) { case 'session_aal2_required' : window . location . href = err . response ?. data . redirect_browser_to ; return ; case 'session_already_available' : await router . push ( '/' ) ; return ; case 'session_refresh_required' : window . location . href = err . response ?. data . redirect_browser_to ; return ; case 'self_service_flow_return_to_forbidden' : toast . error ( 'The return_to address is not allowed.' ) ; resetFlow ( undefined ) ; await router . push ( '/' + flowType ) ; return ; case 'self_service_flow_expired' : toast . error ( 'Your interaction expired, please fill out the form again.' ) ; resetFlow ( undefined ) ; await router . push ( '/' + flowType ) ; return ; case 'security_csrf_violation' : toast . error ( 'A security violation was detected, please fill out the form again.' ) ; resetFlow ( undefined ) ; await router . push ( '/' + flowType ) ; return ; case 'security_identity_mismatch' : resetFlow ( undefined ) ; await router . push ( '/' + flowType ) ; return ; case 'browser_location_change_required' : window . location . href = err . response . data . redirect_browser_to ; return ; } switch ( err . response ?. status ) { case 410 : resetFlow ( undefined ) ; await router . push ( '/' + flowType ) ; return ; } return Promise . reject ( err ) ; } ; } export const handleFlowError = handleGetFlowError ;