-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.jsx
90 lines (82 loc) · 2.31 KB
/
index.jsx
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
import React, { Component } from 'react'
import firebase from 'firebase'
import { HashRouter, Route, Switch } from 'react-router-dom'
import 'normalize-css'
import Header from './header'
import Main from './main'
import Profile from './profile'
import Login from './#'
class App extends Component {
constructor () {
super()
this.state = {
user: null
}
this.handleOnAuth = this.handleOnAuth.bind(this)
this.handleLogout = this.handleLogout.bind(this)
}
componentDidMount () {
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.setState({ user })
} else {
this.setState({ user: null })
}
})
}
handleOnAuth () {
const provider = new firebase.auth.GithubAuthProvider()
firebase.auth().signInWithPopup(provider)
.then(result => console.log(result))
.catch(error => console.error(error))
}
handleLogout () {
firebase.auth().signOut()
.then(() => console.log('You logged out correctly!'))
.catch(() => console.error('Error while trying to logout'))
}
render () {
return (
<HashRouter>
<div>
<Header />
<Switch>
<Route exact path='/' render={() => {
if (this.state.user) {
return (
<Main
user={this.state.user}
onLogout={this.handleLogout}
/>
)
} else {
return (
<Login onAuth={this.handleOnAuth} />
)
}
}} />
<Route exact path='/profile' render={() => {
return (
<Profile
picture={this.state.user.photoURL}
username={this.state.user.email.split('@')[0]}
displayName={this.state.user.displayName}
emailAdress={this.state.user.email}
/>
)
}} />
<Route exact path='/user/:username' render={({ match: { params } }) => {
return (
<Profile
displayName={params.username}
username={params.username}
/>
)
}} />
</Switch>
</div>
</HashRouter>
)
}
}
export default App