Commit fda409bb authored by Matija Obreza's avatar Matija Obreza
Browse files

Restored /login

parent 3ee15e7d
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {saveCookies} from 'utilities';
import * as _ from 'lodash';
import {log} from 'utilities/debug';
import {loginRequest, checkTokenRequest} from 'actions/login';
import ContentHeader from 'ui/common/heading/ContentHeader';
import LoginForm from './c/LoginForm';
import Grid from 'material-ui/Grid';
import Card, { CardHeader, CardContent } from 'ui/common/Card';
interface ILoginContainerProps extends React.ClassAttributes<any> {
login: any;
loginRequest: (u, p) => Promise<any>;
checkTokenRequest: (t) => Promise<any>;
history: any;
}
class LoginContainer extends React.Component<ILoginContainerProps, void> {
private onLogin = (username, password): any => {
const { loginRequest, checkTokenRequest, history } = this.props;
log('Trying login', username);
return loginRequest(username, password)
.then(({access_token}) => {
log('Access token', access_token);
return checkTokenRequest(access_token);
})
.then((data) => {
saveCookies(data);
history.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" />
<Grid container spacing={ 0 } justify="center" className="back-gray p-20">
<Grid item xs={ 12 } md={ 5 } lg={ 4 } xl={ 3 }>
<Card>
<CardHeader title="Login" />
<CardContent>
<LoginForm onTryLogin={ this.onLogin } />
</CardContent>
</Card>
</Grid>
</Grid>
</div>
);
}
}
const mapStateToProps = (state) => ({
login: state.login,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loginRequest,
checkTokenRequest,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);
import * as React from 'react';
import TextField from 'material-ui/TextField';
import Button from 'material-ui/Button';
interface ILoginProps extends React.ClassAttributes<any> {
onTryLogin: (username, password) => Promise<any>;
}
class Login extends React.Component<ILoginProps, any> {
public constructor(props: any) {
super(props);
this.state = {
error: null,
errorDescription: null,
username: '',
password: '',
};
}
protected authorize = (e) => {
const { onTryLogin } = this.props;
e.preventDefault();
if (this.state.username && this.state.password) {
onTryLogin(this.state.username, this.state.password).then((res) => {
if (res) {
this.setState(res);
}
});
}
}
private handleUsername = (e) => this.setState({...this.state, username: e.target.value});
private handlePassword = (e) => this.setState({...this.state, password: e.target.value});
public render() {
return (
<form onSubmit={ this.authorize }>
<TextField
label="Username"
value={ this.state.username }
onChange={ this.handleUsername }
fullWidth
/>
<TextField
label="Password"
type="password"
value={ this.state.password }
onChange={ this.handlePassword }
fullWidth
/>
{ this.state.error && <div style={ { color: 'red' } }>{ this.state.errorDescription }</div> }
<Button
raised
type="submit"
style={ { margin: '1rem 0 1rem 0' } }
>
Login
</Button>
</form>
);
}
}
export default Login;
......@@ -4,6 +4,7 @@ import App from './App';
import Wrapper from 'ui/catalog/Wrapper';
import DashboardPage from './pages/dashboard/DashboardPage';
import LoginPage from './pages/login/LoginPage';
import AccessionsBrowsePage from './pages/genesys/BrowsePage';
import NotFound from './common/not-found';
......@@ -18,6 +19,11 @@ const routes = [
exact: true,
component: WelcomePage,
},
{
path: '/login',
component: LoginPage,
exact: true,
},
{
path: '/accessions',
component: Wrapper,
......
Markdown is supported
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