saleor-dashboard/src/auth/utils.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

import { ApolloError } from "@apollo/client";
2020-07-24 09:17:25 +00:00
import { IMessageContext } from "@saleor/components/messages";
2020-05-25 23:38:52 +00:00
import { UseNotifierResult } from "@saleor/hooks/useNotifier";
import { commonMessages } from "@saleor/intl";
import { IntlShape } from "react-intl";
2020-07-27 09:39:00 +00:00
import { isJwtError, isTokenExpired } from "./errors";
2020-07-24 09:17:25 +00:00
2020-05-25 23:38:52 +00:00
export const displayDemoMessage = (
intl: IntlShape,
notify: UseNotifierResult
) => {
notify({
text: intl.formatMessage(commonMessages.demo)
});
};
2020-07-24 09:17:25 +00:00
export async function handleQueryAuthError(
error: ApolloError,
notify: IMessageContext,
logout: () => void,
intl: IntlShape
) {
if (error.graphQLErrors.some(isJwtError)) {
logout();
2020-07-27 09:39:00 +00:00
if (error.graphQLErrors.every(isTokenExpired)) {
notify({
status: "error",
text: intl.formatMessage(commonMessages.sessionExpired)
});
2020-07-24 09:17:25 +00:00
} else {
notify({
status: "error",
text: intl.formatMessage(commonMessages.somethingWentWrong)
});
}
} else if (
!error.graphQLErrors.every(
err => err.extensions?.exception?.code === "PermissionDenied"
)
) {
notify({
status: "error",
text: intl.formatMessage(commonMessages.somethingWentWrong)
});
}
}