Skip to content

Commit

Permalink
Fix authentication with token (#12348)
Browse files Browse the repository at this point in the history
* #12347 fix token login

* #12347 revert back to fetching data directly from the store
  • Loading branch information
siemiatj committed Jan 21, 2022
1 parent 800cb59 commit 129842e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions 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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/hooks/useAuth.js
Expand Up @@ -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 (
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/routes/TokenRoute.js
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit 129842e

Please sign in to comment.