Skip to content

Commit

Permalink
Add: disable login button after multiple failed attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
georgipavlov-7DIGIT committed Feb 8, 2024
1 parent df468e1 commit edbaab9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function App() {
const [isAnonymousRegister, setIsAnonymousRegister] = useState(false);
const [theme, setTheme] = useState(null);

const [isLoginDisabled, setIsLoginDisabled] = useState(false);

const [dropdownOptions, setDropdownOptions] = useState({
isOpen: false,
options: [],
Expand Down Expand Up @@ -187,6 +189,8 @@ function App() {
setIsAnonymousRegister,
theme,
setTheme,
isLoginDisabled,
setIsLoginDisabled,
};

return (
Expand Down
15 changes: 13 additions & 2 deletions src/blocks/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Login = ({ navigation }) => {
const { t } = useTranslation("login");
const queryClient = useQueryClient();

const { setToken } = useContext(Context);
const { setToken, isLoginDisabled, setIsLoginDisabled } = useContext(Context);

const [data, setData] = useState({
email: "",
Expand Down Expand Up @@ -68,6 +68,17 @@ export const Login = ({ navigation }) => {
},
onError: (error) => {
const { message: errorMessage } = useError(error);
const errorCode = error.response.status;
if (errorCode === 429 && !isLoginDisabled) {
// If the user is rate limited, disable the login button for the remaining cooldown time
const remainingCooldownInSeconds =
error.response.data?.error?.customData?.remainingCooldownInSeconds ||
0;
setIsLoginDisabled(true);
setTimeout(() => {
setIsLoginDisabled(false);
}, remainingCooldownInSeconds * 1000);
}
setErrors({ submit: errorMessage });
},
});
Expand Down Expand Up @@ -133,7 +144,7 @@ export const Login = ({ navigation }) => {
label={t("login_label")}
size="lg"
onPress={handleLogin}
disabled={!data.email || !data.password}
disabled={!data.email || !data.password || isLoginDisabled}
loading={loginMutation.isLoading}
isSubmit
style={styles.loginButton}
Expand Down

0 comments on commit edbaab9

Please sign in to comment.