How to use Hooks confusion
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
I'm working on a page.tsx for a dynamic route trips/[id]. I need to retrieve a document from Firebase based on the logged-in user, using useAuthContext() to access the user object. When I try to use useAuthContext inside the server component, I get the following error:
This code gives error on useAuthContext:
Error: (0 , src_auth_hooksWEBPACK_IMPORTED_MODULE_5.useAuthContext) is not a function
-----------------------------------------------------------------------------------------
import { doc, getDoc } from 'firebase/firestore';
import { CONFIG } from 'src/config-global';
import { FIRESTORE } from 'src/lib/firebase';
import { TripDetailsView } from 'src/sections/trips/view';
import { useAuthContext } from 'src/auth/hooks';
// ----------------------------------------------------------------------
export const metadata = { title:
type Props = {
params: { id: string };
};
export default async function Page({ params }: Props) {
const { id } = params;
const { user } = useAuthContext()
const tripRef = doc(FIRESTORE, 'users', user?.uid, 'trips', id);
const docSnap = await getDoc(tripRef);
const trip = docSnap.data();
return <TripDetailsView trip={trip} />;
}
I can't use the useAuthContext hook inside a server component because React doesn't support hooks like this on the server side. But I need access to the authenticated user (user.uid) to get the trip document from Firestore.
If the circumstances where so that I didn't need the user and I remove the line const { user } = useAuthContext() , then the page works. However, I obviously need that user to fetch the relevant trip data. How can I resolve this?
This code gives error on useAuthContext:
Error: (0 , src_auth_hooksWEBPACK_IMPORTED_MODULE_5.useAuthContext) is not a function
-----------------------------------------------------------------------------------------
import { doc, getDoc } from 'firebase/firestore';
import { CONFIG } from 'src/config-global';
import { FIRESTORE } from 'src/lib/firebase';
import { TripDetailsView } from 'src/sections/trips/view';
import { useAuthContext } from 'src/auth/hooks';
// ----------------------------------------------------------------------
export const metadata = { title:
Trip details - ${CONFIG.appName}
};type Props = {
params: { id: string };
};
export default async function Page({ params }: Props) {
const { id } = params;
const { user } = useAuthContext()
const tripRef = doc(FIRESTORE, 'users', user?.uid, 'trips', id);
const docSnap = await getDoc(tripRef);
const trip = docSnap.data();
return <TripDetailsView trip={trip} />;
}
I can't use the useAuthContext hook inside a server component because React doesn't support hooks like this on the server side. But I need access to the authenticated user (user.uid) to get the trip document from Firestore.
If the circumstances where so that I didn't need the user and I remove the line const { user } = useAuthContext() , then the page works. However, I obviously need that user to fetch the relevant trip data. How can I resolve this?
8 Replies
Sun bear
What auth libary do you use? In general most libaries have a way to access the user clientside and serverside.
In general serverside acccessing should be possible to protect routes
In general serverside acccessing should be possible to protect routes
Asiatic LionOP
firebase/auth
I didn't really build the front-end stuff, it's a bought template, I just follow what it says here, it says this to get the user info
https://docs.minimals.cc/authentication/user-info/
This method works on the other pages that the template has and I use the same logic that it uses on dynamic pages in my own dynamic pages but I also need the user info this time and it is not being done anywhere else similar in the template, where also use the user and async/await to get data from database and don't know how to proceed, maybe some hook like useEffect or a wrapper around the page, I'm not sure
I didn't really build the front-end stuff, it's a bought template, I just follow what it says here, it says this to get the user info
https://docs.minimals.cc/authentication/user-info/
This method works on the other pages that the template has and I use the same logic that it uses on dynamic pages in my own dynamic pages but I also need the user info this time and it is not being done anywhere else similar in the template, where also use the user and async/await to get data from database and don't know how to proceed, maybe some hook like useEffect or a wrapper around the page, I'm not sure
Sun bear
Ah okay. Then i would inspect the useAuthContext function. Maybe you will see how its done. Most likely the user is fetched serverside and passed to the context
@Asiatic Lion so try to see if you have api to get authed user on the server side
if you have, use it instead of the hook
if you can't find the api, just create a client component that does the whole logic inside it, you can pass param id to the client component as a prop, and as it's a client component you will be able to use the hook
useAuthContext()
, right?Asiatic LionOP
@James4u (Tag me if needed) If it's a client component, the useAuthContext() would work, yes, but the docSnap wouldn't work that needs await/async
well, then you should find server api to get auth user