-
Notifications
You must be signed in to change notification settings - Fork 895
fix: handle all auth API errors #3241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
6ae13b1
c37b4ec
865c1c5
c41fb43
3ce260a
dadce4b
3dbd3f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,9 @@ export const Language = { | |
emailInvalid: "Please enter a valid email address.", | ||
emailRequired: "Please enter an email address.", | ||
authErrorMessage: "Incorrect email or password.", | ||
methodsErrorMessage: "Unable to fetch auth methods.", | ||
getUserErrorMessage: "Unable to fetch user details.", | ||
checkPermissionsErrorMessage: "Unable to fetch user permissions.", | ||
getMethodsErrorMessage: "Unable to fetch auth methods.", | ||
passwordSignIn: "Sign In", | ||
githubSignIn: "GitHub", | ||
} | ||
|
@@ -65,11 +67,17 @@ const useStyles = makeStyles((theme) => ({ | |
}, | ||
})) | ||
|
||
type LoginErrors = { | ||
authError?: Error | unknown | ||
getUserError?: Error | unknown | ||
checkPermissionsError?: Error | unknown | ||
getMethodsError?: Error | unknown | ||
} | ||
|
||
export interface SignInFormProps { | ||
isLoading: boolean | ||
redirectTo: string | ||
authError?: Error | unknown | ||
methodsError?: Error | unknown | ||
loginErrors: LoginErrors | ||
authMethods?: AuthMethods | ||
onSubmit: ({ email, password }: { email: string; password: string }) => Promise<void> | ||
// initialTouched is only used for testing the error state of the form. | ||
|
@@ -80,8 +88,7 @@ export const SignInForm: FC<SignInFormProps> = ({ | |
authMethods, | ||
redirectTo, | ||
isLoading, | ||
authError, | ||
methodsError, | ||
loginErrors, | ||
onSubmit, | ||
initialTouched, | ||
}) => { | ||
|
@@ -101,18 +108,39 @@ export const SignInForm: FC<SignInFormProps> = ({ | |
onSubmit, | ||
initialTouched, | ||
}) | ||
const getFieldHelpers = getFormHelpersWithError<BuiltInAuthFormValues>(form, authError) | ||
const getFieldHelpers = getFormHelpersWithError<BuiltInAuthFormValues>( | ||
form, | ||
loginErrors.authError, | ||
) | ||
|
||
return ( | ||
<> | ||
<Welcome /> | ||
<form onSubmit={form.handleSubmit}> | ||
<Stack> | ||
{authError && ( | ||
<ErrorSummary error={authError} defaultMessage={Language.authErrorMessage} /> | ||
{loginErrors.authError && ( | ||
<ErrorSummary | ||
error={loginErrors.authError} | ||
defaultMessage={Language.authErrorMessage} | ||
/> | ||
)} | ||
{loginErrors.getUserError && ( | ||
<ErrorSummary | ||
error={loginErrors.getUserError} | ||
defaultMessage={Language.getUserErrorMessage} | ||
/> | ||
)} | ||
{loginErrors.checkPermissionsError && ( | ||
<ErrorSummary | ||
error={loginErrors.checkPermissionsError} | ||
defaultMessage={Language.checkPermissionsErrorMessage} | ||
/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would DRY this up by mapping over the keys of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought about it, but we’ll have to map default messages in a separate object for that. I’ll try that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking if you map over the keys you could do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declared an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I was thinking of iterating over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that threw some error and I wasn't able to proceed. |
||
)} | ||
{methodsError && ( | ||
<ErrorSummary error={methodsError} defaultMessage={Language.methodsErrorMessage} /> | ||
{loginErrors.getMethodsError && ( | ||
<ErrorSummary | ||
error={loginErrors.getMethodsError} | ||
defaultMessage={Language.getMethodsErrorMessage} | ||
/> | ||
)} | ||
<TextField | ||
{...getFieldHelpers("email")} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ export const LoginPage: React.FC = () => { | |
authSend({ type: "SIGN_IN", email, password }) | ||
} | ||
|
||
const { authError, getUserError, checkPermissionsError, getMethodsError } = authState.context | ||
|
||
if (authState.matches("signedIn")) { | ||
return <Navigate to={redirectTo} replace /> | ||
} else { | ||
|
@@ -54,8 +56,12 @@ export const LoginPage: React.FC = () => { | |
authMethods={authState.context.methods} | ||
redirectTo={redirectTo} | ||
isLoading={isLoading} | ||
authError={authState.context.authError} | ||
methodsError={authState.context.getMethodsError as Error} | ||
loginErrors={{ | ||
authError, | ||
getUserError, | ||
checkPermissionsError, | ||
getMethodsError, | ||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
onSubmit={onSubmit} | ||
/> | ||
</div> | ||
|
Uh oh!
There was an error while loading. Please reload this page.