show toast when oauth login error

This commit is contained in:
Sheen Capadngan
2023-05-18 01:58:35 +08:00
parent 6ca3b8ba61
commit ec8d62d106
3 changed files with 34 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import jwt_decode from 'jwt-decode';
import { useNotificationContext } from '@app/components/context/Notifications/NotificationProvider';
import SecurityClient, { PROVIDER_AUTH_TOKEN_KEY } from '@app/components/utilities/SecurityClient';
export const useProviderAuth = () => {
@@ -8,12 +9,19 @@ export const useProviderAuth = () => {
const [userId, setUserId] = useState<string>('');
const [providerAuthToken, setProviderAuthToken] = useState<string>('');
const [isProviderUserCompleted, setIsProviderUserCompleted] = useState<boolean>();
const { createNotification } = useNotificationContext();
const AUTH_ERROR_KEY = 'PROVIDER_AUTH_ERROR'
useEffect(() => {
SecurityClient.setProviderAuthToken('')
window.localStorage.removeItem(AUTH_ERROR_KEY);
const handleStorageChange = (event: StorageEvent) => {
if (event.storageArea === localStorage && event.key === PROVIDER_AUTH_TOKEN_KEY) {
if (event.storageArea !== localStorage) {
return;
}
if (event.key === PROVIDER_AUTH_TOKEN_KEY) {
if (event.newValue) {
const token = event.newValue;
const {
@@ -33,6 +41,18 @@ export const useProviderAuth = () => {
}
setProviderAuthToken(event.newValue || '');
}
if (event.key === AUTH_ERROR_KEY) {
if (event.newValue) {
createNotification({
text: 'An error has occured during login.',
type: 'error',
timeoutMs: 6000,
})
window.localStorage.removeItem(AUTH_ERROR_KEY);
}
}
};
window.addEventListener('storage', handleStorageChange);

View File

@@ -57,9 +57,11 @@ const App = ({ Component, pageProps, ...appProps }: NextAppProp): JSX.Element =>
) {
return (
<QueryClientProvider client={queryClient}>
<AuthProvider>
<Component {...pageProps} />
</AuthProvider>
<NotificationProvider>
<AuthProvider>
<Component {...pageProps} />
</AuthProvider>
</NotificationProvider>
</QueryClientProvider>
);
}

View File

@@ -1,7 +1,10 @@
import { useEffect } from "react";
export default function LoginProviderError() {
return (
<div className="bg-gradient-to-tr from-bunker-500 to-bunker-800 h-screen flex flex-col justify-center pb-28 px-6 items-center">
<span className="text-white text-xl" >Oops! An error has occured.</span>
</div>
)
useEffect(() => {
window.localStorage.setItem('PROVIDER_AUTH_ERROR', 'err');
window.close();
}, [])
return <div />
}