diff --git a/src/components/Nav/AuthModal/ForgotPasswordForm.tsx b/src/components/Nav/AuthModal/ForgotPasswordForm.tsx index 5fae5e8e..1bf2b12d 100644 --- a/src/components/Nav/AuthModal/ForgotPasswordForm.tsx +++ b/src/components/Nav/AuthModal/ForgotPasswordForm.tsx @@ -12,7 +12,6 @@ import { validateEmail } from '../../../utils/validateEmail' import { email, setEmail } from './sharedLogic' import styles from './AuthModal.module.scss' -import { ApiResponse, ForgotPasswordResponse } from '@authorizerdev/authorizer-js' type FormFields = { email: string @@ -28,7 +27,7 @@ export const ForgotPasswordForm = () => { setEmail(newEmail.toLowerCase()) } const { - actions: { authorizer }, + actions: { forgotPassword }, } = useSession() const [submitError, setSubmitError] = createSignal('') const [isSubmitting, setIsSubmitting] = createSignal(false) @@ -62,22 +61,28 @@ export const ForgotPasswordForm = () => { setIsSubmitting(true) try { - const response: ApiResponse = await authorizer().forgotPassword({ + const { data, errors } = await forgotPassword({ email: email(), redirect_uri: window.location.origin, }) - console.debug('[ForgotPasswordForm] authorizer response:', response) - if (response?.data) setMessage(response.data.message) - else { - console.warn(response.errors) + if (data) { + console.debug('[ForgotPasswordForm] authorizer response:', data) + setMessage(data.message) + } + if (errors) { + console.warn(errors) + if (errors) { + const error: Error = errors[0] + if (error.cause === 'user_not_found') { + setIsUserNotFound(true) + return + } else { + setSubmitError(error.message) + } + } } } catch (error) { console.error(error) - if (error?.code === 'user_not_found') { - setIsUserNotFound(true) - return - } - setSubmitError(error?.message) } finally { setIsSubmitting(false) } diff --git a/src/context/session.tsx b/src/context/session.tsx index 5cdd03e8..c2b8b8f7 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -12,7 +12,8 @@ import { AuthorizeResponse, ApiResponse, GenericResponse, - // GraphqlQueryInput, + ForgotPasswordResponse, + ForgotPasswordInput, } from '@authorizerdev/authorizer-js' import { createContext, @@ -62,6 +63,9 @@ export type SessionContextType = { signIn: (params: LoginInput) => Promise signOut: () => Promise oauth: (provider: string) => Promise + forgotPassword: ( + params: ForgotPasswordInput, + ) => Promise<{ data: ForgotPasswordResponse; errors: Error[] }> changePassword: (password: string, token: string) => void confirmEmail: (input: VerifyEmailInput) => Promise // email confirm callback is in auth.discours.io setIsSessionLoaded: (loaded: boolean) => void @@ -293,6 +297,12 @@ export const SessionProvider = (props: { console.debug('[context.session] change password response:', resp) } + const forgotPassword = async (params: ForgotPasswordInput) => { + const resp = await authorizer().forgotPassword(params) + console.debug('[context.session] change password response:', resp) + return { data: resp?.data, errors: resp.errors } + } + const confirmEmail = async (input: VerifyEmailInput) => { console.debug(`[context.session] calling authorizer's verify email with`, input) try { @@ -338,6 +348,7 @@ export const SessionProvider = (props: { setAuthor, authorizer, loadAuthor, + forgotPassword, changePassword, oauth, }