{"size": 1494, "ext": "js", "max_stars_repo_path": "src/components/App/AuthControls/AuthControls.js", "max_stars_repo_name": "jimyandres/TODO-List", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/App/AuthControls/AuthControls.js", "max_issues_repo_name": "jimyandres/TODO-List", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/App/AuthControls/AuthControls.js", "max_forks_repo_name": "jimyandres/TODO-List", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.6666666667, "max_line_length": 87, "alphanum_fraction": 0.6425702811}
import React, { Component } from 'react';
import LoginHeader from './LoginHeader';
import LoginLinksPanel from './LoginLinksPanel';
import { Route, Switch, Redirect } from 'react-router'
import './AuthControls.css';

class AuthControls extends Component {
  constructor(props){
    super(props)
    this.state = {userData:null}
    this.stitchClient = props.stitchClient;
  }

  componentDidMount() {
    if (this.stitchClient.isAuthenticated()) {
      this.stitchClient.userProfile()
      .then(userData=>{
        this.setState({userData:userData.data})
      })
    }
  }

  render() {
    let isAuthed = this.stitchClient.isAuthenticated();
    let logout = () => this.stitchClient.logout().then(() => window.location.href="/");
    const { userData } = this.state;
    let onOAuthGoogle = () => this.stitchClient.authenticate("google");
    let onOAuthFB = () => this.stitchClient.authenticate("facebook");

    return (
      <AuthControlsWithLoginHeader
        isAuthed={isAuthed}
        onLogout={logout}
        userData={userData}
        onOAuthGoogle={onOAuthGoogle}
        onOAuthFB={onOAuthFB}
      />
    );
  }
};

const withLoginHeader = (Component) =>
  ({isAuthed, ...rest}) =>
    isAuthed
    ? <LoginHeader {...rest} />
    : <Switch>
      <Route exact path='/'render={() => <Component {...rest} />} />
      <Redirect from='/:visibility?' to='/' />
    </Switch>

const AuthControlsWithLoginHeader = withLoginHeader(LoginLinksPanel);

export default AuthControls;
