Created
June 18, 2020 08:07
-
-
Save whisher/e01f24bb427e62e665fe77e9cf02a29f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interceptor | |
const setupAxiosInterceptors = (onUnauthenticated: () => void) => { | |
const onRequestSuccess = (config: AxiosRequestConfig) => { | |
console.log("request success", config); | |
const token = Storage.local.get("auth"); | |
if (token) { | |
config.headers.Authorization = `Bearer ${token}`; | |
} | |
return config; | |
}; | |
const onRequestFail = (error: AxiosError) => { | |
console.log("request error", error); | |
return Promise.reject(error); | |
}; | |
axios.interceptors.request.use(onRequestSuccess, onRequestFail); | |
const onResponseSuccess = (response: AxiosResponse) => { | |
console.log("response success", response); | |
return response; | |
}; | |
const onResponseFail = ( | |
error: { status: number } & { response: { status: number } } | |
) => { | |
const status = error.status || error.response.status; | |
if (status === 403 || status === 401) { | |
onUnauthenticated(); | |
} | |
return Promise.reject(error); | |
}; | |
axios.interceptors.response.use(onResponseSuccess, onResponseFail); | |
}; | |
export default setupAxiosInterceptors; | |
// Slice | |
export const authSlice = createSlice({ | |
name: "auth", | |
initialState: authInitialState as AuthState, | |
reducers: { | |
authLogin: (state) => { | |
return { | |
...state, | |
loading: true, | |
}; | |
}, | |
authLoginFailure: (state) => { | |
return { | |
...state, | |
error: true, | |
loading: false, | |
}; | |
}, | |
authLoginSuccess: (state, action) => { | |
return { | |
...state, | |
error: false, | |
loading: false, | |
token: action.payload, | |
}; | |
}, | |
authLoginRedirect: (state, action) => { | |
return { | |
...state, | |
}; | |
}, | |
authLogout: () => { | |
return authInitialState; | |
}, | |
}, | |
}); | |
// Effects | |
export const authLogoutEffects = () => ( | |
dispatch: ThunkDispatch<{}, {}, AnyAction> | |
) => { | |
dispatch(authLogout()); | |
localStorage.remove("auth"); | |
}; | |
// Store | |
const createRootReducer = (history: History) => | |
combineReducers({ | |
router: connectRouter(history), | |
auth: authReducer, | |
}); | |
const rootReducer = createRootReducer(history); | |
const store = configureStore({ | |
reducer: rootReducer, | |
middleware: [routerMiddleware(history), ...getDefaultMiddleware()], | |
}); | |
export default store; | |
// Index.tsx | |
const { dispatch } = store; | |
setupAxiosInterceptors(() => { | |
dispatch(authLogoutEffects()); | |
}); | |
Error | |
Argument of type '(dispatch: ThunkDispatch<{}, {}, AnyAction>) => void' is not assignable to parameter of type 'AnyAction | LocationChangeAction'. Type '(dispatch: ThunkDispatch<{}, {}, AnyAction>) => void' is missing the following properties from type 'LocationChangeAction': type, payloadts(2345) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment