-
Notifications
You must be signed in to change notification settings - Fork 27.1k
/
index.js
149 lines (135 loc) · 3.8 KB
/
index.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { Component } from 'react'
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'
import 'isomorphic-unfetch'
import clientCredentials from '../credentials/client'
export async function getServerSideProps({ req, query }) {
const user = req && req.session ? req.session.decodedToken : null
// don't fetch anything from firebase if the user is not found
// const snap = user && await req.firebaseServer.database().ref('messages').once('value')
// const messages = snap && snap.val()
const messages = null
return {
props: {
user,
messages,
},
}
}
export default class Index extends Component {
constructor(props) {
super(props)
this.state = {
user: this.props.user,
value: '',
messages: this.props.messages,
}
this.addDbListener = this.addDbListener.bind(this)
this.removeDbListener = this.removeDbListener.bind(this)
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
componentDidMount() {
firebase.initializeApp(clientCredentials)
if (this.state.user) this.addDbListener()
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.setState({ user: user })
return user
.getIdToken()
.then(token => {
// eslint-disable-next-line no-undef
return fetch('/api/login', {
method: 'POST',
// eslint-disable-next-line no-undef
headers: new Headers({ 'Content-Type': 'application/json' }),
credentials: 'same-origin',
body: JSON.stringify({ token }),
})
})
.then(res => this.addDbListener())
} else {
this.setState({ user: null })
// eslint-disable-next-line no-undef
fetch('/api/logout', {
method: 'POST',
credentials: 'same-origin',
}).then(() => this.removeDbListener())
}
})
}
addDbListener() {
var db = firebase.firestore()
let unsubscribe = db.collection('messages').onSnapshot(
querySnapshot => {
var messages = {}
querySnapshot.forEach(function(doc) {
messages[doc.id] = doc.data()
})
if (messages) this.setState({ messages })
},
error => {
console.error(error)
}
)
this.setState({ unsubscribe })
}
removeDbListener() {
// firebase.database().ref('messages').off()
if (this.state.unsubscribe) {
this.state.unsubscribe()
}
}
handleChange(event) {
this.setState({ value: event.target.value })
}
handleSubmit(event) {
event.preventDefault()
var db = firebase.firestore()
const date = new Date().getTime()
db.collection('messages')
.doc(`${date}`)
.set({
id: date,
text: this.state.value,
})
this.setState({ value: '' })
}
handleLogin() {
firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())
}
handleLogout() {
firebase.auth().signOut()
}
render() {
const { user, value, messages } = this.state
return (
<div>
{user ? (
<button onClick={this.handleLogout}>Logout</button>
) : (
<button onClick={this.handleLogin}>Login</button>
)}
{user && (
<div>
<form onSubmit={this.handleSubmit}>
<input
type={'text'}
onChange={this.handleChange}
placeholder={'add message...'}
value={value}
/>
</form>
<ul>
{messages &&
Object.keys(messages).map(key => (
<li key={key}>{messages[key].text}</li>
))}
</ul>
</div>
)}
</div>
)
}
}