You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing an application with React Native and I want to set up a middleware that refreshes the token if it is expired (jwt refresh token). I am also using thunk for asynchronous functions.
If token is expired, I push the current action in array and I call the refresh token method in dispatcher. When the refresh token is renew, I call all actions in array.
My problem is that when the token expired, I use store.dispatch (userActions.refreshToken(state.auth.user.refresh_token)). Unfortunately when I launch the app I got no error just a white screen.
If I use next (userActions.refreshToken (state.auth.user.refresh_token)) instead it works. Why not with store.dispatch
constauthMiddleware=store=>next=>action=>{conststate=store.getState()letisTokenExpired=falseif(action.type==='REFRESH_TOKEN_REQUEST'){returnnext(action)}if(action.type==='REFRESH_TOKEN_SUCCESS'){next(action)if(buffer.length>0){console.log("buffer : "+buffer)buffer.forEach(action=>store.dispatch(action))buffer=[]}}if(typeofaction==='function'){// Si il y a pas de token user c'est qu'on est en cours de connexion.if(!state.auth.user.token){returnnext(action)}isTokenExpired=isExpired(state.auth.user.token)if(isTokenExpired){// MY PROBLEM IS HERE ==>store.dispatch(userActions.refreshToken(state.auth.user.refresh_token))buffer.push(action)}}returnnext(action)
Refresh token méthode
functionrefreshToken(refreshToken){returnasyncdispatch=>{dispatch(request())authApi.refreshToken(refreshToken).then((res)=>{constuser=resdispatch(success(user))},(err)=>{showMessage({message: err.message,type: "danger",floating: true,duration: 3500});dispatch(failure())})}functionrequest(){return{type: 'REFRESH_TOKEN_REQUEST'}}functionsuccess(user){return{type: 'REFRESH_TOKEN_SUCCESS', user }}functionfailure(){return{type: 'REFRESH_TOKEN_FAILURE'}}}
This discussion was converted from issue #4246 on December 30, 2021 16:22.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
I am developing an application with React Native and I want to set up a middleware that refreshes the token if it is expired (jwt refresh token). I am also using thunk for asynchronous functions.
If token is expired, I push the current action in array and I call the refresh token method in dispatcher. When the refresh token is renew, I call all actions in array.
My problem is that when the token expired, I use store.dispatch (userActions.refreshToken(state.auth.user.refresh_token)). Unfortunately when I launch the app I got no error just a white screen.
If I use next (userActions.refreshToken (state.auth.user.refresh_token)) instead it works. Why not with store.dispatch
package.json
AuthMiddleware
Refresh token méthode
configureStore
Thanks in advance for your great help
Beta Was this translation helpful? Give feedback.
All reactions