Encountering the error
ReferenceError: window is not defined
when attempting to set up Firebase analytics in my Next.js project.
Below is the code snippet causing the issue:
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let analytics;
if (window !== undefined) {
analytics = getAnalytics(app);
}
onAuthStateChanged(auth, (user) => {
if (user) {
// User signed in
const uid = user.uid;
console.log(uid);
} else {
// User signed out.
}
});
export { app, auth, analytics };
A solution for this problem was discussed in a previous thread; however, the suggested fix does not seem to be effective in my case.