Procházet zdrojové kódy

refresh token bug frontend

pull/129/head
Dzenis Hadzifejzovic před 3 roky
rodič
revize
e5da8f7927
4 změnil soubory, kde provedl 96 přidání a 90 odebrání
  1. 1
    13
      src/App.js
  2. 62
    48
      src/AppRoutes.js
  3. 7
    3
      src/pages/CandidatesPage/CandidatesPage.js
  4. 26
    26
      src/store/saga/loginSaga.js

+ 1
- 13
src/App.js Zobrazit soubor

import React, { useEffect } from "react";
import React from "react";
import { Router } from "react-router-dom"; import { Router } from "react-router-dom";
import { Helmet } from "react-helmet-async"; import { Helmet } from "react-helmet-async";
import i18next from "i18next"; import i18next from "i18next";
import history from "./store/utils/history"; import history from "./store/utils/history";
import MainContainer from "./components/Section/MainContainer"; import MainContainer from "./components/Section/MainContainer";
import AppRoutes from "./AppRoutes"; import AppRoutes from "./AppRoutes";
import { useDispatch } from "react-redux";
import { refreshUserToken } from "./store/actions/login/loginActions";
import { BASE_PAGE } from "./constants/pages";


function App() { function App() {
const dispatch = useDispatch();

useEffect(() => {
if (history.location.pathname === BASE_PAGE) {
return;
}
dispatch(refreshUserToken());
}, []);

return ( return (
<> <>
<Router history={history}> <Router history={history}>

+ 62
- 48
src/AppRoutes.js Zobrazit soubor

import React from "react";
import React, { useEffect } from "react";
import { Redirect, Route, Switch } from "react-router-dom"; import { Redirect, Route, Switch } from "react-router-dom";
import { useDispatch } from "react-redux";
import { refreshUserToken } from "./store/actions/login/loginActions";
import { useLocation } from "react-router-dom";


import { import {
HOME_PAGE, HOME_PAGE,
SCHEDULE_PAGE, SCHEDULE_PAGE,
STATS_PAGE, STATS_PAGE,
REGISTER_PAGE, REGISTER_PAGE,
CREATE_AD_PAGE
CREATE_AD_PAGE,
} from "./constants/pages"; } from "./constants/pages";


// import LoginPage from './pages/LoginPage/LoginPage'; // import LoginPage from './pages/LoginPage/LoginPage';
import RegisterPage from "./pages/RegisterPage/RegisterPage"; import RegisterPage from "./pages/RegisterPage/RegisterPage";
import CreateAdPage from "./pages/AdsPage/CreateAdPage"; import CreateAdPage from "./pages/AdsPage/CreateAdPage";


const AppRoutes = () => (
<Switch>
<Route exact path={BASE_PAGE} component={LoginPage} />
<Route path={NOT_FOUND_PAGE} component={NotFoundPage} />
{/* <Route path={USERS_PAGE} component={UsersPage} /> */}
<Route path={ERROR_PAGE} component={ErrorPage} />
<Route path={FORGOT_PASSWORD_PAGE} component={ForgotPasswordPage} />
<Route
path={FORGOT_PASSWORD_CONFIRMATION_PAGE}
component={ForgotPasswordConfirmationPage}
/>
<Route exact path={REGISTER_PAGE} component={RegisterPage} />
<Route path={RESET_PASSWORD_PAGE} component={ResetPasswordPage} />
<PrivateRoute exact path={HOME_PAGE} component={HomePage} />
<PrivateRoute exact path={ADS_PAGE} component={AdsPage} />
<PrivateRoute exact path={AD_DETAILS_PAGE} component={AdDetailsPage} />
<PrivateRoute exact path={USER_DETAILS_PAGE} component={UserDetails} />
<PrivateRoute exact path={USERS_PAGE} component={UsersPage} />
<PrivateRoute exact path={CANDIDATES_PAGE} component={CandidatesPage} />
<PrivateRoute exact path={CREATE_AD_PAGE} component={CreateAdPage} />
<PrivateRoute
exact
path={CANDIDATES_DETAILS_PAGE}
component={CandidateDetailsPage}
/>
<PrivateRoute
exact
path={SELECTION_PROCESS_PAGE}
component={SelectionProcessPage}
/>
<PrivateRoute
exact
path={SELECTION_PROCESS_OF_APPLICANT_PAGE}
component={SelectionProcessOfApplicantPage}
/>
<PrivateRoute
exact
path={PATTERN_DETAILS_PAGE}
component={PatternDetailsPage}
/>
<PrivateRoute exact path={PATTERNS_PAGE} component={PatternsPage} />
<PrivateRoute exact path={SCHEDULE_PAGE} component={SchedulePage} />
<PrivateRoute exact path={STATS_PAGE} component={StatsPage} />
<Redirect from="*" to={NOT_FOUND_PAGE} />
</Switch>
);
const AppRoutes = () => {
const dispatch = useDispatch();
const location = useLocation();

useEffect(() => {
if (location.pathname === BASE_PAGE) {
return;
}
dispatch(refreshUserToken());
}, [location]);
return (
<Switch>
<Route exact path={BASE_PAGE} component={LoginPage} />
<Route path={NOT_FOUND_PAGE} component={NotFoundPage} />
{/* <Route path={USERS_PAGE} component={UsersPage} /> */}
<Route path={ERROR_PAGE} component={ErrorPage} />
<Route path={FORGOT_PASSWORD_PAGE} component={ForgotPasswordPage} />
<Route
path={FORGOT_PASSWORD_CONFIRMATION_PAGE}
component={ForgotPasswordConfirmationPage}
/>
<Route exact path={REGISTER_PAGE} component={RegisterPage} />
<Route path={RESET_PASSWORD_PAGE} component={ResetPasswordPage} />
<PrivateRoute exact path={HOME_PAGE} component={HomePage} />
<PrivateRoute exact path={ADS_PAGE} component={AdsPage} />
<PrivateRoute exact path={AD_DETAILS_PAGE} component={AdDetailsPage} />
<PrivateRoute exact path={USER_DETAILS_PAGE} component={UserDetails} />
<PrivateRoute exact path={USERS_PAGE} component={UsersPage} />
<PrivateRoute exact path={CANDIDATES_PAGE} component={CandidatesPage} />
<PrivateRoute exact path={CREATE_AD_PAGE} component={CreateAdPage} />
<PrivateRoute
exact
path={CANDIDATES_DETAILS_PAGE}
component={CandidateDetailsPage}
/>
<PrivateRoute
exact
path={SELECTION_PROCESS_PAGE}
component={SelectionProcessPage}
/>
<PrivateRoute
exact
path={SELECTION_PROCESS_OF_APPLICANT_PAGE}
component={SelectionProcessOfApplicantPage}
/>
<PrivateRoute
exact
path={PATTERN_DETAILS_PAGE}
component={PatternDetailsPage}
/>
<PrivateRoute exact path={PATTERNS_PAGE} component={PatternsPage} />
<PrivateRoute exact path={SCHEDULE_PAGE} component={SchedulePage} />
<PrivateRoute exact path={STATS_PAGE} component={StatsPage} />
<Redirect from="*" to={NOT_FOUND_PAGE} />
</Switch>
);
};


export default AppRoutes; export default AppRoutes;

+ 7
- 3
src/pages/CandidatesPage/CandidatesPage.js Zobrazit soubor

) : ( ) : (
<AdsCandidatesPage history={history} search={search} /> <AdsCandidatesPage history={history} search={search} />
)} )}
<Fade in={isCVDisplayed} timeout={400} className="candidates-cv">
<embed src={`data:application/pdf;base64,${linkToCV}`} />
</Fade>
{isTableView ? (
<Fade in={isCVDisplayed} timeout={400} className="candidates-cv">
<embed src={`data:application/pdf;base64,${linkToCV}`} />
</Fade>
) : (
""
)}
</div> </div>
</div> </div>
); );

+ 26
- 26
src/store/saga/loginSaga.js Zobrazit soubor

import { all, call, put, takeLatest } from '@redux-saga/core/effects';
import jwt from 'jsonwebtoken';
import history from '../utils/history';
import { all, call, put, takeLatest } from "@redux-saga/core/effects";
import jwt from "jsonwebtoken";
import history from "../utils/history";
import { import {
AUTHENTICATE_USER, AUTHENTICATE_USER,
LOGIN_GOOGLE_USER_FETCH, LOGIN_GOOGLE_USER_FETCH,
REFRESH_TOKEN, REFRESH_TOKEN,
GENERATE_TOKEN, GENERATE_TOKEN,
FORGOT_PASSWORD, FORGOT_PASSWORD,
RESET_PASSWORD
} from '../actions/login/loginActionConstants';
RESET_PASSWORD,
} from "../actions/login/loginActionConstants";
import { import {
attemptGoogleLogin, attemptGoogleLogin,
attemptLogin, attemptLogin,
refreshTokenRequest, refreshTokenRequest,
generateTokenRequest, generateTokenRequest,
forgetPasswordEmailSend, forgetPasswordEmailSend,
sendResetPassword
} from '../../request/loginRequest';
sendResetPassword,
} from "../../request/loginRequest";
import { import {
fetchGoogleUserSuccess, fetchGoogleUserSuccess,
fetchUserError, fetchUserError,
forgetPasswordSuccess, forgetPasswordSuccess,
resetLoginState, resetLoginState,
updateUserToken, updateUserToken,
} from '../actions/login/loginActions';
import { BASE_PAGE } from '../../constants/pages';
import { setUser } from '../actions/user/userActions';
import {
addHeaderToken,
removeHeaderToken,
} from '../../request';
} from "../actions/login/loginActions";
import { BASE_PAGE } from "../../constants/pages";
import { setUser } from "../actions/user/userActions";
import { addHeaderToken, removeHeaderToken } from "../../request";
import { import {
IMPERSONATE_USER_UID, IMPERSONATE_USER_UID,
REGISTRATION_USER_UID, REGISTRATION_USER_UID,
} from '../../constants/sessionStorage';
} from "../../constants/sessionStorage";
import { import {
JWT_REFRESH_TOKEN, JWT_REFRESH_TOKEN,
JWT_TOKEN, JWT_TOKEN,
REFRESH_TOKEN_CONST, REFRESH_TOKEN_CONST,
} from '../../constants/localStorage';
} from "../../constants/localStorage";
import { import {
authScopeClearHelper, authScopeClearHelper,
authScopeStringGetHelper, authScopeStringGetHelper,
authScopeRemoveHelper, authScopeRemoveHelper,
authScopeSetHelper, authScopeSetHelper,
} from '../../util/helpers/authScopeHelpers';
import { rejectErrorCodeHelper } from '../../util/helpers/rejectErrorCodeHelper';
} from "../../util/helpers/authScopeHelpers";
import { rejectErrorCodeHelper } from "../../util/helpers/rejectErrorCodeHelper";


function* fetchUser({ payload }) { function* fetchUser({ payload }) {
try { try {
const errorMessage = yield call(rejectErrorCodeHelper, e); const errorMessage = yield call(rejectErrorCodeHelper, e);
yield put(fetchUserError(errorMessage)); yield put(fetchUserError(errorMessage));
} }
}
finally{
} finally {
// console.log('Done') // console.log('Done')
} }
} }
if (data.token) { if (data.token) {
// const user = jwt.decode(data.token); // const user = jwt.decode(data.token);
yield call(authScopeSetHelper, JWT_TOKEN, data.token); yield call(authScopeSetHelper, JWT_TOKEN, data.token);
yield call(authScopeSetHelper, JWT_REFRESH_TOKEN, data.JwtRefreshToken);
yield call(authScopeSetHelper, REFRESH_TOKEN_CONST, data.RefreshToken);
yield call(authScopeSetHelper, JWT_REFRESH_TOKEN, data.refreshToken);
yield call(authScopeSetHelper, REFRESH_TOKEN_CONST, data.refreshToken);

yield call(addHeaderToken, data.token); yield call(addHeaderToken, data.token);
yield put(setUser(data)); yield put(setUser(data));
} }
return yield put( return yield put(
fetchUserSuccess({ fetchUserSuccess({
JwtToken, JwtToken,
}),
})
); );
} catch (error) { } catch (error) {
const errorMessage = yield call(rejectErrorCodeHelper, error); const errorMessage = yield call(rejectErrorCodeHelper, error);
export function* refreshToken() { export function* refreshToken() {
try { try {
const token = yield call(authScopeStringGetHelper, JWT_TOKEN); const token = yield call(authScopeStringGetHelper, JWT_TOKEN);
const refreshToken = yield call(authScopeStringGetHelper,REFRESH_TOKEN_CONST);
const refreshToken = yield call(
authScopeStringGetHelper,
REFRESH_TOKEN_CONST
);


if (token && refreshToken) { if (token && refreshToken) {
const { data } = yield call(refreshTokenRequest, { const { data } = yield call(refreshTokenRequest, {
yield put(setUser(data.data)); yield put(setUser(data.data));
} }
} catch (error) { } catch (error) {
localStorage.removeItem(JWT_TOKEN)
localStorage.removeItem(REFRESH_TOKEN_CONST)
localStorage.removeItem(JWT_TOKEN);
localStorage.removeItem(REFRESH_TOKEN_CONST);
yield call(history.replace, BASE_PAGE); yield call(history.replace, BASE_PAGE);
} }
} }

Načítá se…
Zrušit
Uložit