LoginPage.tsx 3.37 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
4
5
6
7
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as _ from 'lodash';

import {log} from 'utilities/debug';

8
import {loginRequest, checkTokenRequest, verifyGoogleTokenRequest} from 'user/actions/public';
Matija Obreza's avatar
Matija Obreza committed
9
10
11

import ContentHeader from 'ui/common/heading/ContentHeader';

Matija Obreza's avatar
Matija Obreza committed
12
import PageLayout from 'ui/layout/PageLayout';
Matija Obreza's avatar
Matija Obreza committed
13
import LoginForm from './c/LoginForm';
Matija Obreza's avatar
Matija Obreza committed
14
import Grid from '@material-ui/core/Grid';
Matija Obreza's avatar
Matija Obreza committed
15
16
17
18
19
20
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>;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21
    verifyGoogleTokenRequest: (aT) => Promise<any>;
Matija Obreza's avatar
Matija Obreza committed
22
    history: any;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
23
    googleClientId: string;
Matija Obreza's avatar
Matija Obreza committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

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) => {
              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});
              }
      });
    }

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    private onGoogleLogin = (response): any => {
        const { verifyGoogleTokenRequest, checkTokenRequest, history } = this.props;
        log('Trying google login');
        return verifyGoogleTokenRequest(response.accessToken)
            .then(({access_token}) => {
                log('Access token', access_token);
                return checkTokenRequest(access_token);
            })
            .then((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});
                }
            });
    }

Matija Obreza's avatar
Matija Obreza committed
66
67
    public render() {
        return (
Oleksii Savran's avatar
Oleksii Savran committed
68
              <PageLayout withFooter>
Matija Obreza's avatar
Matija Obreza committed
69
70
71
72
73
74
                <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>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
75
                        <LoginForm onTryLogin={ this.onLogin }  onTryGoogleLogin={ this.onGoogleLogin } googleClientId={ this.props.googleClientId } />
Matija Obreza's avatar
Matija Obreza committed
76
77
78
79
80
                      </CardContent>
                    </Card>
                  </Grid>
                </Grid>
            </PageLayout>
Matija Obreza's avatar
Matija Obreza committed
81
82
83
84
85
86
        );
    }
}

const mapStateToProps = (state) => ({
    login: state.login,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87
    googleClientId: state.applicationConfig.googleClientId,
Matija Obreza's avatar
Matija Obreza committed
88
89
90
91
92
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
    loginRequest,
    checkTokenRequest,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
93
    verifyGoogleTokenRequest,
Matija Obreza's avatar
Matija Obreza committed
94
95
96
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);