Commit 38e27468 authored by Matija Obreza's avatar Matija Obreza
Browse files

LoginForm is a simple control, logic is in the containing LoginPage

- If cookies['authorities'] cannot be parsed as JSON, remove the cookie
- replaced require() with import in utilities/index.ts
parent a89e2d9c
......@@ -6,7 +6,11 @@ import {ROLE_CLIENT} from 'constants/userRoles';
let authorities = [ROLE_CLIENT];
if (typeof window !== 'undefined' && cookies.get('authorities')) {
authorities = JSON.parse(cookies.get('authorities'));
try {
authorities = JSON.parse(cookies.get('authorities'));
} catch (e) {
cookies.remove('authorities');
}
}
const INITIAL_STATE = {
......
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {saveCookies} from 'utilities';
import * as _ from 'lodash';
import {loginRequest, checkTokenRequest} from 'actions/login';
import {loginRequest, checkTokenRequest, verifyGoogleTokenRequest} from 'actions/login';
import Container from 'ui/common/container';
import ContentHeader from 'ui/common/heading/ContentHeader';
import Heading from 'ui/common/heading';
import Login from './Login';
import LoginForm from './c/LoginForm';
interface ILoginContainerProps extends React.ClassAttributes<any> {
login: any;
loginRequest: (u, p) => Promise<any>;
checkTokenRequest: (t) => Promise<any>;
verifyGoogleTokenRequest: (accessToken, googleClientId) => Promise<any>;
router: any;
}
class LoginContainer extends React.Component<ILoginContainerProps, void> {
public render() {
const {login, loginRequest, checkTokenRequest, verifyGoogleTokenRequest, router} = this.props;
private onLogin = (username, password): any => {
const { loginRequest, checkTokenRequest, router } = this.props;
console.log('Trying login', username);
return loginRequest(username, password)
.then(({access_token}) => {
console.log('Access token', access_token);
return checkTokenRequest(access_token);
})
.then((data) => {
saveCookies(data);
router.push('/dashboard');
return false;
}).catch((e) => {
const data = _.get(e, 'response.data');
if (data && data.error) {
return ({error: data.error, errorDescription: data.error_description});
}
});
}
public render() {
return (
<div>
<ContentHeader title="Welcome to Genesys" subTitle="Log in to manage datasets" />
<Container testid="login" size={ 4 } center>
<Heading title="Login"/>
<Login login={ login } checkTokenRequest={ checkTokenRequest }
loginRequest={ loginRequest } router={ router }
verifyGoogleTokenRequest={ verifyGoogleTokenRequest } />
<LoginForm onTryLogin={ this.onLogin } />
</Container>
</div>
);
}
}
......@@ -40,7 +61,6 @@ const mapStateToProps = (state) => ({
const mapDispatchToProps = (dispatch) => bindActionCreators({
loginRequest,
checkTokenRequest,
verifyGoogleTokenRequest,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);
......@@ -3,16 +3,8 @@ import * as _ from 'lodash';
import TextField from 'material-ui/TextField';
import Button from 'material-ui/Button';
import {saveCookies} from 'utilities';
const googleClientId: string = process.env.GOOGLE_CLIENT_ID;
interface ILoginProps extends React.ClassAttributes<any> {
login: any;
loginRequest: (u, p) => Promise<any>;
checkTokenRequest: (t) => Promise<any>;
verifyGoogleTokenRequest: (accessToken, googleClientId) => Promise<any>;
router: any;
onTryLogin: (username, password) => Promise<any>;
}
class Login extends React.Component<ILoginProps, any> {
......@@ -29,33 +21,15 @@ class Login extends React.Component<ILoginProps, any> {
}
protected authorize = (e) => {
const { onTryLogin } = this.props;
e.preventDefault();
this.props.loginRequest(this.state.username, this.state.password)
.then(({access_token}) => this.props.checkTokenRequest(access_token))
.then((data) => {
saveCookies(data);
this.props.router.push('/dashboard');
}).catch((e) => {
const data = _.get(e, 'response.data');
if (data && data.error) {
this.setState({error: data.error, errorDescription: data.error_description});
if (this.state.username && this.state.password) {
onTryLogin(this.state.username, this.state.password).then((res) => {
if (res) {
this.setState(res);
}
});
}
private onSuccessGoogle = (response) => {
this.props.verifyGoogleTokenRequest(response.tokenId, googleClientId)
.then(({access_token}) => this.props.checkTokenRequest(access_token))
.then((data) => {
saveCookies(data);
this.props.router.push('/dashboard');
}).catch((e) => {
this.setState({error: e.error, errorDescription: e.error_description});
});
}
private onFailureGoogle = (response) => {
this.setState({error: response.error, errorDescription: response.details});
}
}
private handleUsername = (e) => this.setState({...this.state, username: e.target.value});
......
......@@ -7,7 +7,7 @@ import App from './App';
import AuthorizedRouteComponent from './common/authorized/AuthorizedRouteComponent';
import WelcomePage from './pages/welcome';
import Login from './pages/login';
import LoginPage from './pages/login/LoginPage';
import PartnerWrapper from './pages/partner/Wrapper';
import PartnerBrowsePage from './pages/partner/BrowsePage';
......@@ -96,7 +96,7 @@ export default(
<Route path=":tab" component={ DashboardPage } />
</Route>
</Route>
<Route path="/login" component={ Login }/>
<Route path="/login" component={ LoginPage }/>
{ guiTestingRoutes() }
<Route path="*" component={ NotFound }/>
</Route>
......
import * as cookies from 'es-cookie';
export function noTime(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
......@@ -66,13 +67,11 @@ export function arraysEqual(a: any[], b: any[]): boolean {
}
export function saveCookies(resp) {
const cookies = require('es-cookie');
cookies.set('access_token', resp.access_token, { path: '/' });
cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/' });
}
export function clearCookies() {
const cookies = require('es-cookie');
['authorities', 'access_token'].forEach((key) => cookies.remove(key));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment