1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Firebase server side auth example update: don't fetch messages server-side if user is not found (#3087)

* don't fetch messages server-side if user is not found 

Currently, the component always fetch everything under '/messages' even if the user is not authenticated on the server side. Update it to not fetch if the user is not found as a better example on handling.

* fix lint error
This commit is contained in:
Sitian Liu 2017-10-13 17:08:41 -04:00 committed by Tim Neutkens
parent 63ac27b900
commit a25cb412bf

View file

@ -6,8 +6,10 @@ import clientCredentials from '../credentials/client'
export default class Index extends Component {
static async getInitialProps ({req, query}) {
const user = req && req.session ? req.session.decodedToken : null
const snap = await req.firebaseServer.database().ref('messages').once('value')
return { user, messages: snap.val() }
// 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()
return { user, messages }
}
constructor (props) {