Skip to content

Commit a25cb41

Browse files
goldensunliutimneutkens
authored andcommitted
Firebase server side auth example update: don't fetch messages server-side if user is not found (vercel#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
1 parent 63ac27b commit a25cb41

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

  • examples/with-firebase-authentication/pages

examples/with-firebase-authentication/pages/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import clientCredentials from '../credentials/client'
66
export default class Index extends Component {
77
static async getInitialProps ({req, query}) {
88
const user = req && req.session ? req.session.decodedToken : null
9-
const snap = await req.firebaseServer.database().ref('messages').once('value')
10-
return { user, messages: snap.val() }
9+
// don't fetch anything from firebase if the user is not found
10+
const snap = user && await req.firebaseServer.database().ref('messages').once('value')
11+
const messages = snap && snap.val()
12+
return { user, messages }
1113
}
1214

1315
constructor (props) {

0 commit comments

Comments
 (0)