Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/containers/Authentication/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Authentication({closable = false}: AuthenticationProps) {
const history = useHistory();
const location = useLocation();

const [authenticate, {error, isLoading}] = authenticationApi.useAuthenticateMutation(undefined);
const [authenticate, {isLoading}] = authenticationApi.useAuthenticateMutation(undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't it work as expected?
I mean isn't auth mutation supposed to return error on error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutation result is always {data, error}, it cannot be processed by catch without .unwrap(). In previous version code in .then always ran, that caused the bug with error being ignored. I could use only .then as well - .then(({data, error}) -> {}), but form with explicit .catch looks better for me


const {returnUrl} = parseQuery(location);

Expand All @@ -34,15 +34,6 @@ function Authentication({closable = false}: AuthenticationProps) {
const [passwordError, setPasswordError] = React.useState('');
const [showPassword, setShowPassword] = React.useState(false);

React.useEffect(() => {
if (isUserError(error)) {
setLoginError(error.data.error);
}
if (isPasswordError(error)) {
setPasswordError(error.data.error);
}
}, [error]);

const onLoginUpdate = (value: string) => {
setLogin(value);
setLoginError('');
Expand All @@ -54,18 +45,28 @@ function Authentication({closable = false}: AuthenticationProps) {
};

const onLoginClick = () => {
authenticate({user: login, password}).then(() => {
if (returnUrl) {
const decodedUrl = decodeURIComponent(returnUrl.toString());

// to prevent page reload we use router history
// history navigates relative to origin
// so we remove origin to make it work properly
const url = new URL(decodedUrl);
const path = url.pathname + url.search;
history.replace(path);
}
});
authenticate({user: login, password})
.unwrap()
.then(() => {
if (returnUrl) {
const decodedUrl = decodeURIComponent(returnUrl.toString());

// to prevent page reload we use router history
// history navigates relative to origin
// so we remove origin to make it work properly
const url = new URL(decodedUrl);
const path = url.pathname + url.search;
history.replace(path);
}
})
.catch((error) => {
if (isUserError(error)) {
setLoginError(error.data.error);
}
if (isPasswordError(error)) {
setPasswordError(error.data.error);
}
});
};

const onEnterClick = (e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
Expand Down
Loading