You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide seems to no longer work with nextjs 10+.
Tested using code with my own specifications and copy paste from this repo.
I also tested using both nookies and js-cookies
What happens:
Token gets set correctly on login page, I can see the token in the applications panel of my chrome devtools. Whenever the token is used in SSR the token is shown as invalid by firebase and deleted from the front end cookie storage.
When trying to use this token on a SSR page the token dissapears, firebase gives an error stating
code: 'auth/argument-error',
message: 'First argument to verifyIdToken() must be a Firebase ID token string.'
My auth.tsx (at this point copy paste from this repo, tried replacing nookies with js-cookies but got the same issue)
//auth.tsxconstAuthContext=createContext<{user: firebaseClient.User|null}>({user: null,});exportfunctionAuthProvider({ children }: any){const[user,setUser]=useState<firebaseClient.User|null>(null);useEffect(()=>{if(typeofwindow!=="undefined"){(windowasany).nookies=nookies;}returnfirebaseClient.auth().onIdTokenChanged(async(user)=>{console.log(`token changed!`);if(!user){console.log(`no token found...`);setUser(null);nookies.destroy(null,"token");nookies.set(null,"token","",{path: '/'});return;}console.log(`updating token...`);consttoken=awaituser.getIdToken();setUser(user);nookies.destroy(null,"token");nookies.set(null,"token",token,{path: '/'});});},[]);useEffect(()=>{consthandle=setInterval(async()=>{console.log(`refreshing token...`);constuser=firebaseClient.auth().currentUser;if(user)awaituser.getIdToken(true);},10*60*1000);return()=>clearInterval(handle);},[]);return(<AuthContext.Providervalue={{ user }}>{children}</AuthContext.Provider>);}exportconstuseAuth=()=>{returnuseContext(AuthContext);}
//authenticatedPage.tsxexportconstgetServerSideProps=async(req,ctx)=>{try{constcookies=nookies.get(ctx);//Always fails hereconsttoken=awaitfirebaseAdmin.auth().verifyIdToken(cookies.token);console.log("Token is:",token)return{props: {data: "Worked!"},};}catch(err){constcookies=nookies.get(ctx);return{props: {data: "Did not worked!"},};}};constRevisions=({ data, ctx })=>{const{ user }=useAuth();return(<Layout><Container>{/* always returns no user signed in */}<p>{`User ID: ${user ? user.uid : 'no user signed in'}`}</p><p>test</p></Container></Layout>);};exportdefaultRevisions
Edit:
Tested with NextJS 11, same issue.
It seems like the issue happens in auth.tsx when token is being unset if no user is present. The token gets unset when ran un an SSR page.
returnfirebaseClient.auth().onIdTokenChanged(async(user)=>{if(!user){setUser(null);// Cookies.set("token", "Satt fra greia");// nookies.set(null, "token", "",);return;}
by commenting out the destruction of the cookie things begin working. But this seems to be like a very sub-optimal and possible insecure solution.
The text was updated successfully, but these errors were encountered:
The issue comes from Firebase deleting cookies when using Firebase Functions or Cloud Run, as it only allows for using a special-named cookie called __session, try updating the name of the cookie from token to __session and it might do the trick.
This guide seems to no longer work with nextjs 10+.
Tested using code with my own specifications and copy paste from this repo.
I also tested using both nookies and js-cookies
What happens:
Token gets set correctly on login page, I can see the token in the applications panel of my chrome devtools. Whenever the token is used in SSR the token is shown as invalid by firebase and deleted from the front end cookie storage.
When trying to use this token on a SSR page the token dissapears, firebase gives an error stating
My auth.tsx (at this point copy paste from this repo, tried replacing nookies with js-cookies but got the same issue)
Relevant versions:
What have I tried?
Shifting from nookies to js-cookies
Using the encode option with nookies
Edit:
Tested with NextJS 11, same issue.
It seems like the issue happens in auth.tsx when token is being unset if no user is present. The token gets unset when ran un an SSR page.
by commenting out the destruction of the cookie things begin working. But this seems to be like a very sub-optimal and possible insecure solution.
The text was updated successfully, but these errors were encountered: