Next.js Discord

Discord Forum

firebase auth + nextjs failed

Unanswered
Pip gall wasp posted this in #help-forum
Open in Discord
Pip gall waspOP
Unable to receieve any user data after successful login

27 Replies

Pip gall waspOP
Pip gall waspOP
also tried replacing the popup fn with redirect fn still getting same error
@Pip gall wasp also tried replacing the popup fn with redirect fn still getting same error
if the user exists and you can see it in the network tab, i think you should try moving your console.log inside the unsubscribe
Pip gall waspOP
there is no user
so how do you know the login was successful?
Pip gall waspOP
because I can see the user been registered in the firebase
the user you're logging is the user in the useState not the user in the onAuthStateChanged callback
or better still add the user in the dep array in your useEffect
Pip gall waspOP
import { useEffect } from "react";
import { useAuth } from "./auth";
import { useRouter } from "next/router";

export default function withAuth(Component) {
return function WithAuthComponent(props) {
const { loading, user } = useAuth();
const router = useRouter();
useEffect(() => {
if (loading && !user) {
router.push("/login");
} else if (!loading && user) {
router.push("/");
}
}, [loading, user, router]);

if (loading) return <div>Loading...</div>;
return <Component {...props} />;
};
}
am i doing something wrong in this
not this useEffect
this

useEffect(() => {
    const unsubscribe = onAuthStateChanged(auth, (user) => {
      setUser(user);
      setLoading(false);
    });
    console.log(user ?? "no data");
    return () => unsubscribe();
  }, []);
Pip gall waspOP
so I'm getting mulitple user objects idk why and also the redirect is not working after clicking on login button
I think something is wrong either in auth.js or withAuth.js
yes, with your useEffects
what does your useEffect look like in both places atm?
Pip gall waspOP
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
setUser(user || null);
setLoading(false);
});
return () => unsubscribe();
}, []);
try commenting out the second useEffect and log in again and see if you still get an infinite loop
Pip gall waspOP
import withAuth from "@/provider/withAuth";
import "@/styles/globals.css";

function App({ Component, pageProps }) {
return <Component {...pageProps} />;
}

export default withAuth(App);
means the problem is with the useEffect
why not move your redirect to the login function and handle everything there since you already have your loading states there too
Pip gall waspOP
didn't work srry
Pip gall waspOP
but thanks you were the only one who came for support 🙂