2017-04-22 12:56:28 +00:00
|
|
|
import React, { Component } from 'react'
|
2018-12-10 13:25:40 +00:00
|
|
|
import firebase from 'firebase/app'
|
|
|
|
import 'firebase/auth'
|
|
|
|
import 'firebase/firestore'
|
2017-11-04 14:05:16 +00:00
|
|
|
import 'isomorphic-unfetch'
|
2017-05-08 19:53:48 +00:00
|
|
|
import clientCredentials from '../credentials/client'
|
2017-04-22 12:56:28 +00:00
|
|
|
|
|
|
|
export default class Index extends Component {
|
2018-12-17 16:34:32 +00:00
|
|
|
static async getInitialProps ({ req, query }) {
|
2017-04-22 12:56:28 +00:00
|
|
|
const user = req && req.session ? req.session.decodedToken : null
|
2017-10-13 21:08:41 +00:00
|
|
|
// don't fetch anything from firebase if the user is not found
|
2018-12-10 13:25:40 +00:00
|
|
|
// const snap = user && await req.firebaseServer.database().ref('messages').once('value')
|
|
|
|
// const messages = snap && snap.val()
|
|
|
|
const messages = null
|
2017-10-13 21:08:41 +00:00
|
|
|
return { user, messages }
|
2017-04-22 12:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
user: this.props.user,
|
|
|
|
value: '',
|
|
|
|
messages: this.props.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addDbListener = this.addDbListener.bind(this)
|
2018-12-10 13:25:40 +00:00
|
|
|
this.removeDbListener = this.removeDbListener.bind(this)
|
2017-04-22 12:56:28 +00:00
|
|
|
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 })
|
2018-12-17 16:34:32 +00:00
|
|
|
return user
|
|
|
|
.getIdToken()
|
|
|
|
.then(token => {
|
2017-04-22 12:56:28 +00:00
|
|
|
// 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 })
|
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
})
|
|
|
|
.then(res => this.addDbListener())
|
2017-04-22 12:56:28 +00:00
|
|
|
} else {
|
|
|
|
this.setState({ user: null })
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
fetch('/api/logout', {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'same-origin'
|
2018-12-10 13:25:40 +00:00
|
|
|
}).then(() => this.removeDbListener())
|
2017-04-22 12:56:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
addDbListener () {
|
2018-12-10 13:25:40 +00:00
|
|
|
var db = firebase.firestore()
|
|
|
|
// Disable deprecated features
|
|
|
|
db.settings({
|
|
|
|
timestampsInSnapshots: true
|
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
2018-12-10 13:25:40 +00:00
|
|
|
this.setState({ unsubscribe })
|
|
|
|
}
|
|
|
|
|
|
|
|
removeDbListener () {
|
|
|
|
// firebase.database().ref('messages').off()
|
2018-12-17 16:34:32 +00:00
|
|
|
if (this.state.unsubscribe) {
|
|
|
|
this.state.unsubscribe()
|
|
|
|
}
|
2017-04-22 12:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange (event) {
|
|
|
|
this.setState({ value: event.target.value })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit (event) {
|
|
|
|
event.preventDefault()
|
2018-12-10 13:25:40 +00:00
|
|
|
var db = firebase.firestore()
|
|
|
|
// Disable deprecated features
|
|
|
|
db.settings({
|
|
|
|
timestampsInSnapshots: true
|
|
|
|
})
|
2017-04-22 12:56:28 +00:00
|
|
|
const date = new Date().getTime()
|
2018-12-17 16:34:32 +00:00
|
|
|
db.collection('messages')
|
|
|
|
.doc(`${date}`)
|
|
|
|
.set({
|
|
|
|
id: date,
|
|
|
|
text: this.state.value
|
|
|
|
})
|
2017-04-22 12:56:28 +00:00
|
|
|
this.setState({ value: '' })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLogin () {
|
|
|
|
firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLogout () {
|
|
|
|
firebase.auth().signOut()
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { user, value, messages } = this.state
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
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>
|
|
|
|
)
|
2017-04-22 12:56:28 +00:00
|
|
|
}
|
|
|
|
}
|