I am trying to have login page with using react typescript which i am using state management with Mobx and also make refresh token method as follow, my problem is before token expires out, the refresh token cant be trigerred by token time ends up. my login method is in userstore.tsx like as:
login = async (creds: UserFormValues) => {
try {
const user = await agent.Account.login(creds);
store.commonStore.setToken(user.token);
this.startRefreshTokenTimer(user);
runInAction(() => this.user = user);
window.location.href = '/dashboard'
} catch (error) {
throw error;
}
}
login method works without any hesitation after that refresh token timer works and runs refreshtoken method as follow;
refreshToken = async () => {
this.stopRefreshTokenTimer();
try {
const user = await agent.Account.refreshToken();
runInAction(() => this.user = user);
store.commonStore.setToken(user.token);
this.startRefreshTokenTimer(user);
} catch (error) {
console.log(error);
}
}
private startRefreshTokenTimer(user: User) {
const jwtToken = JSON.parse(atob(user.token.split('.')[1]));
const expires = new Date(jwtToken.exp * 1000);
const timeout = expires.getTime() - Date.now();
alert(expires.getTime() - Date.now())
this.refreshTokenTimeout = setTimeout(this.refreshToken, timeout);
}
private stopRefreshTokenTimer() {
clearTimeout(this.refreshTokenTimeout);
}
after time is up, i was waiting, refreshtoken method was trigerred but it wouldnt be possible after i gave even 1 min short token expire time and waited 1 min for triggered itself. my root component App.tsx is also as follow;
import 'devextreme/dist/css/dx.softblue.css';
import { observer } from 'mobx-react-lite';
import { Suspense } from 'react'
import { Outlet } from 'react-router-dom'
import { I18nProvider } from '../_metronic/i18n/i18nProvider'
import { LayoutProvider, LayoutSplashScreen } from '../_metronic/layout/core'
import { MasterInit } from '../_metronic/layout/MasterInit'
const App = observer(() => {
return (
<Suspense fallback={<LayoutSplashScreen />}>
<I18nProvider>
<LayoutProvider>
<Outlet />
<MasterInit />
</LayoutProvider>
</I18nProvider>
</Suspense>
)
})
export { App }
I think i skipped some thing i have to do, if anybody helps and wants me to be clearer and extra details, i can give more information about the issue which i got stuck.