File size: 3,274 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// @flow
import * as React from 'react';
import { getItemFromStorage, storeItem } from 'src/helpers/localStorage';
import { withRouter } from 'react-router';
import queryString from 'query-string';
import { SERVER_URL, CLIENT_URL } from 'src/api/constants';
import { Container } from './style';
import { TwitterSigninButton } from './twitter';
import { FacebookSigninButton } from './facebook';
import { GoogleSigninButton } from './google';
import { GithubSigninButton } from './github';

type Props = {
  redirectPath: ?string,
  location: Object,
  githubOnly?: boolean,
};

export type ButtonProps = {
  onClickHandler?: ?Function,
  href: string,
  preferred: boolean,
  showAfter: boolean,
  githubOnly?: boolean,
};

class LoginButtonSet extends React.Component<Props> {
  saveLoginMethod = (type: string) => {
    return storeItem('preferred_signin_method', type);
  };

  render() {
    const { redirectPath, location, githubOnly } = this.props;

    let r;
    if (location) {
      const searchObj = queryString.parse(this.props.location.search);
      r = searchObj.r;
    }

    const postAuthRedirectPath =
      redirectPath !== undefined || r !== undefined
        ? // $FlowFixMe
          `${redirectPath || r}`
        : `${CLIENT_URL}/home`;

    const preferredSigninMethod = getItemFromStorage('preferred_signin_method');

    let nonePreferred = false;
    if (!preferredSigninMethod) {
      nonePreferred = true;
    }

    return (
      <React.Fragment>
        <Container>
          {!githubOnly && (
            <React.Fragment>
              <TwitterSigninButton
                onClickHandler={this.saveLoginMethod}
                href={`${SERVER_URL}/auth/twitter?r=${postAuthRedirectPath}`}
                preferred={
                  nonePreferred ? true : preferredSigninMethod === 'twitter'
                }
                showAfter={preferredSigninMethod === 'twitter'}
              />

              <FacebookSigninButton
                onClickHandler={this.saveLoginMethod}
                href={`${SERVER_URL}/auth/facebook?r=${postAuthRedirectPath}`}
                preferred={
                  nonePreferred ? true : preferredSigninMethod === 'facebook'
                }
                showAfter={preferredSigninMethod === 'facebook'}
              />

              <GoogleSigninButton
                onClickHandler={this.saveLoginMethod}
                href={`${SERVER_URL}/auth/google?r=${postAuthRedirectPath}`}
                preferred={
                  nonePreferred ? true : preferredSigninMethod === 'google'
                }
                showAfter={preferredSigninMethod === 'google'}
              />
            </React.Fragment>
          )}

          <GithubSigninButton
            githubOnly={githubOnly}
            onClickHandler={this.saveLoginMethod}
            href={`${SERVER_URL}/auth/github?r=${postAuthRedirectPath}`}
            preferred={
              githubOnly
                ? true
                : nonePreferred
                ? true
                : preferredSigninMethod === 'github'
            }
            showAfter={preferredSigninMethod === 'github'}
          />
        </Container>
      </React.Fragment>
    );
  }
}

export default withRouter(LoginButtonSet);