question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SSR] Accessing res.locals.user always returns null at nuxtServerInit

See original GitHub issue

First, congratulations for this nice module with a bunch of options. But…

I’m facing serious problems with Firebase Auth and SSR. Even after following each part of the the SSR tutorial, my nuxtServerInit always has res.locals.user empty.

What I’ve done:

  • I installed all dependencies, including @nuxtjs/pwa.
  • I enabled ssr in nuxt.config.js, including credential and a big sessionLifetime value.
  • I setup my action onAuthStateChanged, my nuxtServerInit, and of course all the other related Vuex things, following the online demo repo.
  • I can successfully invoke this.$fireAuth.signInWithEmailAndPassword and receive user data. The onAuthStateChanged is successfully called on any client-side operation I realize with the auth.
  • Inspired on #69, I implemented watcher to look at authUser and perform route redirection when needed, but it just redirects in client-side, causing my server incorrectly redirecting / to /login, and the client back to / after a while. The login page even appears to the user before redirection with this approach!
  • I also have middlewares using store.state.authUser, but they are called only on server for the first request… And since the server don’t have res.locals.user when nuxtServerInit is called, store.state.authUser is always empty on server.

So, after all day struggling with it I’m giving up and asking for help! I can invite to take a look at my repo if needed, I just can’t make it public right now.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:37 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
smakmancommented, Mar 29, 2020

@lupas I got it working on Heroku! Running into some other deployment issues with Firebase right now but as long as you make sure you copy over the contents of the static folder into the public folder I think it should be working just fine. Thanks for your help!

0reactions
wobsorianocommented, Jul 18, 2020

Missing or insufficient permissions

Do you have a solution for this? Apparently if you have a security rule, you will get insufficient permissions. Here’s a sample rule to get insufficient permissions:

match /users/{userId} {   
      allow read, update: if request.auth.uid == userId;
      allow create: if request.auth.uid == userId;
}

but when you do allow read, write: if true;, the error is gone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt SSR Firebase -> Permission denied - Stack Overflow
I have successfully set up the way to dispatch onAuthStateChanged on nuxtServerInit. It seems to work as res.locals.user - object contains ...
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
When you initially access the site, you want to authenticate that token against an API, which returns some basic user info if valid...
Read more >
@nuxtjs/firebase social auth - DEV Community ‍ ‍
This tutorial allow you to have the user data on server side, but won't allow you to do authentified calls to firebase (such...
Read more >
Firebase Auth with SSR
Reason for this is that the Firebase JS SDK is a client-side library that is ... now access the authUser under the res.locals.user...
Read more >
res.locals Property in Express.js - Tutorialspoint
res.locals Property in Express.js - The res.locals is an object that contains the local variables for the response which are scoped to the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found