Advertisement
mrbranden

App.js

Aug 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './App.css';
  4. import firebase from "./firebase"
  5. import Header from './components/Header';
  6. import MainBody from './components/MainBody';
  7. import Footer from './components/Footer';
  8. import RegForm from './components/RegForm';
  9. import LoginBox from './components/LoginBox';
  10.  
  11. class App extends React.Component {
  12.  
  13.   constructor(props) {
  14.     super(props)
  15.  
  16.     // the initial application state
  17.     this.state = {
  18.       user: null
  19.     }
  20.   }
  21.  
  22.   // App "actions" (functions that modify state)
  23.   signIn(username, password) {
  24.  
  25.     this.setState({
  26.       user: {
  27.         username,
  28.         password
  29.       }
  30.     })
  31.   }
  32.  
  33.   signOut() {
  34.     this.setState({user: null})
  35.   }
  36.  
  37.   googleSignIn() {
  38.  
  39.     console.log("HERE");
  40.       // Call Firebase
  41.       let provider = new firebase.auth.GoogleAuthProvider();
  42.       firebase.auth().signInWithPopup(provider).then(function(result) {
  43.         var token = result.credential.accessToken;
  44.         var username = result.user.email;
  45.         console.log("Success! Username: " + username);
  46.  
  47.         this.setState({
  48.         user: {
  49.             username
  50.           }
  51.         })
  52.       }).catch(function(error) {
  53.         var errorCode = error.code;
  54.         var errorMessage = error.message;
  55.         var email = error.email
  56.         var credential = error.credential;
  57.           console.log(errorMessage);
  58.       });
  59.   }
  60.  
  61.   render() {
  62.     return (
  63.       <div className="App">
  64.         <Header />
  65.         {
  66.           (this.state.user) ?
  67.             <Welcome
  68.               user={this.props.user.username}
  69.               onSignOut={this.props.signOut.bind(this)}
  70.             />
  71.           :
  72.             <LoginBox
  73.               onGoogleSignIn={this.googleSignIn.bind(this)}
  74.               onSignIn={this.signIn.bind(this)}
  75.              
  76.             />
  77.         }
  78.         <MainBody user={this.state.user}/>
  79.         <Footer />
  80.       </div>
  81.     );
  82.   }
  83. }
  84.  
  85. ReactDOM.render(<App/>, document.getElementById("root"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement