import React from 'react'; import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; import './connectpage.scss'; import { Session } from '../model/'; import { Container, NgIf, Loader, Notification } from '../components/'; import { ForkMe, RememberMe, Credentials, Form } from './connectpage/'; import { cache, notify } from '../helpers/'; import config from '../../config_client'; import { Alert } from '../components/'; export class ConnectPage extends React.Component { constructor(props){ super(props); this.state = { credentials: {}, remember_me: window.localStorage.hasOwnProperty('credentials') ? true : false, loading: false, doing_a_third_party_login: false }; } componentDidMount(){ // adapted from: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript function getParam(name) { const regex = new RegExp("[?]" + name.replace(/[\[\]]/g, "\\$&") + "(=([^]*)|&|#|$)"); const results = regex.exec(window.location.href); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } // dropbox login if(getParam('state') === 'dropbox'){ this.authenticate({bearer: getParam('access_token'), type: 'dropbox'}); this.setState({doing_a_third_party_login: true}); } // google drive login if(getParam('code')){ this.authenticate({code: getParam('code'), type: 'gdrive'}); this.setState({doing_a_third_party_login: true}); } } authenticate(params){ this.setState({loading: true}); Session.authenticate(params) .then((ok) => { cache.destroy(); const path = params.path && /^\//.test(params.path)? /\/$/.test(params.path) ? params.path : params.path+'/' : '/'; this.props.history.push('/files'+path); }) .catch((err) => { this.setState({loading: false}); notify.send(err, 'error'); }); } initiateAuthToThirdParty(source){ if(source === 'dropbox'){ this.setState({loading: true}); Session.url('dropbox').then((url) => { window.location.href = url; }).catch((err) => { this.setState({loading: false}); notify.send(err, 'error'); }); }else if(source === 'google'){ this.setState({loading: true}); Session.url('gdrive').then((url) => { window.location.href = url; }).catch((err) => { this.setState({loading: false}); notify.send(err, 'error'); }); } } onFormSubmit(data, credentials){ this.setState({credentials: credentials}, () => { this.authenticate(data); }); } setRemember(state){ this.setState({remember_me: state}); } setCredentials(creds){ this.setState({credentials: creds}); } render() { return (