import {
createActionCreators,
createJWT,
JWTState,
remove,
set,
TokenObject
} from 'redux-saga-jwt'
const jwt = createJWT({
*setTokens(state) {
yield Storage.setToken(state);
},
*getTokens() {
return (yield Storage.getToken()) as JWTState;
},
*refreshToken(id, token) {
const newToken = yield LoginService.refresh(token.refreshToken!);
yield put(set(id, newToken as TokenObject))
},
})
const selectors = jwt.createSelectors('main')
const actionCreators = createActionCreators('main')
const rootReducer = combineReducers({
jwt: jwt.reducer,
})
const sagaMiddleware = createSagaMiddleware()
sagaMiddleware.run(function* () {
yield all([
jwt.saga,
])
})
const login = async () => {
const token = await LoginService.login();
dispatch(actionCreators.set(token))
}
const logout = async () => {
await LoginService.login();
dispatch(actionCreators.remove())
}
import {useSelector} from 'react-redux'
const isInitialized = useSelector(selectors.isInitialized)
const isAuthenticated = useSelector(selectors.isAuthenticated)
import {createActionCreators, useDispatch} from "react-redux";
const actionCreators = createActionCreators('main')
const dispatch = useDispatch();
dispatch(actionCreators.set({
...(YOUR_TOKEN),
refreshInterval: -1
}))