mirror of
https://github.com/usatiuk/writer.git
synced 2025-10-29 08:27:49 +01:00
persist jwt token
This commit is contained in:
5
frontend/package-lock.json
generated
5
frontend/package-lock.json
generated
@@ -7402,6 +7402,11 @@
|
|||||||
"symbol-observable": "^1.2.0"
|
"symbol-observable": "^1.2.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"redux-persist": {
|
||||||
|
"version": "5.10.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redux-persist/-/redux-persist-5.10.0.tgz",
|
||||||
|
"integrity": "sha512-sSJAzNq7zka3qVHKce1hbvqf0Vf5DuTVm7dr4GtsqQVOexnrvbV47RWFiPxQ8fscnyiuWyD2O92DOxPl0tGCRg=="
|
||||||
|
},
|
||||||
"redux-saga": {
|
"redux-saga": {
|
||||||
"version": "0.16.2",
|
"version": "0.16.2",
|
||||||
"resolved": "https://registry.npmjs.org/redux-saga/-/redux-saga-0.16.2.tgz",
|
"resolved": "https://registry.npmjs.org/redux-saga/-/redux-saga-0.16.2.tgz",
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
"react-router-dom": "^4.3.1",
|
"react-router-dom": "^4.3.1",
|
||||||
"react-spring": "^7.2.7",
|
"react-spring": "^7.2.7",
|
||||||
"redux": "^4.0.1",
|
"redux": "^4.0.1",
|
||||||
|
"redux-persist": "^5.10.0",
|
||||||
"redux-saga": "^0.16.2"
|
"redux-saga": "^0.16.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ export class AuthScreenComponent extends React.PureComponent<IAuthScreenProps> {
|
|||||||
public render() {
|
public render() {
|
||||||
const { location } = this.props.history;
|
const { location } = this.props.history;
|
||||||
const { from } = this.props.location.state || { from: "/" };
|
const { from } = this.props.location.state || { from: "/" };
|
||||||
console.log(from);
|
|
||||||
const { loggedIn } = this.props;
|
const { loggedIn } = this.props;
|
||||||
return loggedIn ? (
|
return loggedIn ? (
|
||||||
<Redirect to={from} />
|
<Redirect to={from} />
|
||||||
|
|||||||
@@ -1,20 +1,23 @@
|
|||||||
import "./App.scss";
|
|
||||||
import "@blueprintjs/core/lib/css/blueprint.css";
|
import "@blueprintjs/core/lib/css/blueprint.css";
|
||||||
import "@blueprintjs/icons/lib/css/blueprint-icons.css";
|
import "@blueprintjs/icons/lib/css/blueprint-icons.css";
|
||||||
import "normalize.css/normalize.css";
|
import "normalize.css/normalize.css";
|
||||||
|
import "~App.scss";
|
||||||
|
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { render } from "react-dom";
|
import { render } from "react-dom";
|
||||||
import { Provider } from "react-redux";
|
import { Provider } from "react-redux";
|
||||||
import { BrowserRouter } from "react-router-dom";
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import { PersistGate } from "redux-persist/integration/react";
|
||||||
import { App } from "~App";
|
import { App } from "~App";
|
||||||
import { store } from "~redux/store";
|
import { persistor, store } from "~redux/store";
|
||||||
|
|
||||||
render(
|
render(
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<BrowserRouter>
|
<PersistGate loading={null} persistor={persistor}>
|
||||||
<App />
|
<BrowserRouter>
|
||||||
</BrowserRouter>
|
<App />
|
||||||
|
</BrowserRouter>
|
||||||
|
</PersistGate>
|
||||||
</Provider>,
|
</Provider>,
|
||||||
document.getElementById("body"),
|
document.getElementById("body"),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const defaultAuthState: IAuthState = {
|
|||||||
formSpinner: false,
|
formSpinner: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const auth: Reducer<IAuthState, AuthAction> = (
|
export const authReducer: Reducer<IAuthState, AuthAction> = (
|
||||||
state: IAuthState = defaultAuthState,
|
state: IAuthState = defaultAuthState,
|
||||||
action: AuthAction,
|
action: AuthAction,
|
||||||
) => {
|
) => {
|
||||||
|
|||||||
@@ -1,8 +1,17 @@
|
|||||||
import { combineReducers } from "redux";
|
import { combineReducers } from "redux";
|
||||||
import { auth, IAuthState } from "~redux/auth/reducer";
|
import { persistReducer } from "redux-persist";
|
||||||
|
import storage from "redux-persist/lib/storage";
|
||||||
|
import { authReducer, IAuthState } from "~redux/auth/reducer";
|
||||||
|
|
||||||
export interface IAppState {
|
export interface IAppState {
|
||||||
auth: IAuthState;
|
auth: IAuthState;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const rootReducer = combineReducers({ auth });
|
const authPersistConfig = {
|
||||||
|
key: "jwt",
|
||||||
|
storage,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const rootReducer = combineReducers({
|
||||||
|
auth: persistReducer(authPersistConfig, authReducer),
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
import { applyMiddleware, createStore } from "redux";
|
import { applyMiddleware, createStore } from "redux";
|
||||||
|
import { persistStore } from "redux-persist";
|
||||||
import createSagaMiddlware from "redux-saga";
|
import createSagaMiddlware from "redux-saga";
|
||||||
import { rootReducer } from "~redux/reducers";
|
import { rootReducer } from "~redux/reducers";
|
||||||
|
|
||||||
|
import { setToken } from "./api/utils";
|
||||||
import { authSaga } from "./auth/sagas";
|
import { authSaga } from "./auth/sagas";
|
||||||
|
|
||||||
const sagaMiddleware = createSagaMiddlware();
|
const sagaMiddleware = createSagaMiddlware();
|
||||||
|
|
||||||
export const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
|
export const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
|
||||||
|
|
||||||
|
export const persistor = persistStore(store, null, () => {
|
||||||
|
const state = store.getState();
|
||||||
|
if (state.auth.jwt) {
|
||||||
|
setToken(state.auth.jwt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
sagaMiddleware.run(authSaga);
|
sagaMiddleware.run(authSaga);
|
||||||
|
|||||||
Reference in New Issue
Block a user