From 129842eadb361f6afa67bb9f93333d41e10afb25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Siemi=C4=85tkowski?= Date: Fri, 21 Jan 2022 15:25:14 +0100 Subject: [PATCH] Fix authentication with token (#12348) * #12347 fix token login * #12347 revert back to fetching data directly from the store --- frontend/src/containers/App.js | 8 +++++--- frontend/src/hooks/useAuth.js | 4 +++- frontend/src/routes/TokenRoute.js | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/frontend/src/containers/App.js b/frontend/src/containers/App.js index edce90b86d3..b6f925f6841 100644 --- a/frontend/src/containers/App.js +++ b/frontend/src/containers/App.js @@ -1,7 +1,7 @@ import axios from 'axios'; import counterpart from 'counterpart'; import React from 'react'; -import { useDispatch, useStore } from 'react-redux'; +import { useDispatch, useSelector, useStore } from 'react-redux'; import '../assets/css/styles.css'; import { @@ -44,6 +44,7 @@ const App = () => { const auth = useAuth(); const dispatch = useDispatch(); const store = useStore(); + const language = useSelector((state) => state.appHandler.me.language); useConstructor(() => { // this.pluginsRegistry = new PluginsRegistry(this); @@ -121,7 +122,7 @@ const App = () => { } //if not logged in - if (!auth.isLoggedIn && !store.getState().appHandler.loggedIn) { + if (!auth.isLoggedIn && !store.getState().appHandler.isLogged) { return auth.checkAuthentication().then((authenticated) => { if (authenticated) { history.push(location.pathname); @@ -194,7 +195,8 @@ const App = () => { getAvailableLang().then((response) => { const { defaultValue, values } = response.data; const valuesFlatten = values.map((item) => Object.keys(item)[0]); - if (!store.getState().appHandler.me.language) { + + if (!language) { dispatch(setLanguages(values)); } const lang = diff --git a/frontend/src/hooks/useAuth.js b/frontend/src/hooks/useAuth.js index ceea7169406..8b98a7a2395 100644 --- a/frontend/src/hooks/useAuth.js +++ b/frontend/src/hooks/useAuth.js @@ -98,7 +98,9 @@ function useProvideAuth() { setAuthRequestPending(true); return loginWithToken(token) - .then(async () => await login()) + .then( + async () => await login().then(() => setAuthRequestPending(false)) + ) .catch((error) => { // user already logged in error if ( diff --git a/frontend/src/routes/TokenRoute.js b/frontend/src/routes/TokenRoute.js index 8e0bb3c0cf3..e2b17911d63 100644 --- a/frontend/src/routes/TokenRoute.js +++ b/frontend/src/routes/TokenRoute.js @@ -2,9 +2,9 @@ import React from 'react'; import { isEqual } from 'lodash'; import PropTypes from 'prop-types'; import { useStore } from 'react-redux'; +import { useHistory } from 'react-router-dom'; import { useAuth } from '../hooks/useAuth'; -import history from '../services/History'; /** * @file Function component. @@ -16,10 +16,13 @@ import history from '../services/History'; const TokenRoute = ({ match }) => { const { tokenId } = match.params; const auth = useAuth(); + const history = useHistory(); const state = useStore().getState(); if (!auth.authRequestPending() && !state.appHandler.isLogged) { - auth.tokenLogin(tokenId).then(() => history.push('/')); + auth.tokenLogin(tokenId).then(() => { + history.push('/'); + }); } return null;