Skip to content

Commit

Permalink
feat(auth): added support for redirecting to initial page after login (
Browse files Browse the repository at this point in the history
…#797)

close #719
  • Loading branch information
grosswilerp authored and tchiotludo committed Oct 24, 2021
1 parent 96e80e1 commit b360724
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 11 additions & 5 deletions client/src/components/Root/Root.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from 'react';
import { Component } from 'react';
import axios from 'axios';
import { get, put, post, remove } from '../../utils/api';

Expand All @@ -7,6 +7,12 @@ class Root extends Component {
cancel = axios.CancelToken.source();

componentWillUnmount() {
const pathname = window.location.pathname;

if (pathname !== '/ui/login') {
sessionStorage.setItem('returnTo', pathname + (window.location.search || ''));
}

this.cancelAxiosRequests();
}

Expand All @@ -21,16 +27,16 @@ class Root extends Component {
}

getApi(url) {
return get(url, {cancelToken: this.cancel.token})
return get(url, { cancelToken: this.cancel.token })
}
postApi(url, body) {
return post(url, body,{cancelToken: this.cancel.token})
return post(url, body, { cancelToken: this.cancel.token })
}
putApi(url, body) {
return put(url, body,{cancelToken: this.cancel.token})
return put(url, body, { cancelToken: this.cancel.token })
}
removeApi(url, body) {
return remove(url, body, {cancelToken: this.cancel.token})
return remove(url, body, { cancelToken: this.cancel.token })
}

}
Expand Down
20 changes: 12 additions & 8 deletions client/src/containers/Login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import logo from '../../images/logo.svg';
import {uriCurrentUser, uriLogin, uriOidc} from '../../utils/endpoints';
import { uriCurrentUser, uriLogin, uriOidc } from '../../utils/endpoints';
import { organizeRoles } from '../../utils/converters';
import { login } from '../../utils/api';
import Form from '../../components/Form/Form';
Expand Down Expand Up @@ -59,9 +59,13 @@ class Login extends Form {
sessionStorage.setItem('user', currentUserData.username);
sessionStorage.setItem('roles', organizeRoles(currentUserData.roles));

const returnTo = sessionStorage.getItem('returnTo');
sessionStorage.removeItem('returnTo');

this.props.history.push({
pathname: '/ui',
pathname: (returnTo || '/ui'),
});

window.location.reload(true);
} else {
toast.error('Wrong Username or Password!');
Expand All @@ -70,9 +74,9 @@ class Login extends Form {

componentDidMount() {
const auths = JSON.parse(sessionStorage.getItem('auths'));
if(auths && auths.loginEnabled) {
const {loginEnabled, ...config} = auths;
this.setState({config});
if (auths && auths.loginEnabled) {
const { loginEnabled, ...config } = auths;
this.setState({ config });
} else {
this.props.history.push({
pathname: '/ui',
Expand All @@ -86,9 +90,9 @@ class Login extends Form {
<>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text">
<i className="fa fa-user" />
</span>
<span className="input-group-text">
<i className="fa fa-user" />
</span>
</div>
<input
type="text"
Expand Down

0 comments on commit b360724

Please sign in to comment.