Server components confusion
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
I have a component in nextjs and I wanna be able to turn that into a server component because I'm dealing with sensitive data and I do not want this to be accessed from the browser, I'm using the pages directory and in the docs it said that I do not need to change on anything however I still can see the request being sent in the network tab of my app.
export async function getServerSideProps(ctx: any) {
const supabase = createServerSupabaseClient(ctx);
let user = {}; // Default value if fetching from Supabase fails
let plans = {};
const { data: userData, error } = await supabase.auth.getSession();
if (userData?.session?.user.id) {
try {
const getUserUrl = `${process.env.NEXT_PUBLIC_CLIENT_URL}/api/supabase/getUserById?API_ROUTE_SECRET=${process.env.NEXT_PUBLIC_API_ROUTE_SECRET}`;
const userRequestData = { id: userData?.session?.user.id };
const getUserResponse = await fetch(getUserUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userRequestData),
});
const userProfile = await getUserResponse.json();
if (userProfile) {
user = userProfile;
console.log("userProfile", userProfile);
const getPriceUrl = `${process.env.NEXT_PUBLIC_CLIENT_URL}/api/get-price?API_ROUTE_SECRET=${process.env.NEXT_PUBLIC_API_ROUTE_SECRET}`;
const getPriceResponse = await fetch(getPriceUrl);
const plansResponse = await getPriceResponse.json();
plans = plansResponse;
}
} catch (error) {
console.error("Error fetching investigating status:", error);
}
}
return {
props: {
user,
plans,
},
};
}
const Home = ({ user }: any) => {
const supabase = useSupabaseClient();
const router = useRouter();
useEffect(() => {
if (user) {
router.push("/dashboard");
}
}, [user]);
return (
//jsx
);
};
export default Home;7 Replies
Spectacled bearOP
I do not want to use the getServerSideprops
European sprat
Pages directory doesn't have server components
Asian black bear
@Spectacled bear It is not really clear what you are trying to accomplish. If you move to the app dir and use a server component, this would not actually be more secure than
getServerSidePropsYour client component is making requests because of this:
If you do not want to access supabase from the client, all of this code needs to be in
const supabase = useSupabaseClient();
const router = useRouter();If you do not want to access supabase from the client, all of this code needs to be in
getServerSideProps@European sprat Pages directory doesn't have server components
Spectacled bearOP
ohh
@Asian black bear <@547810225777016834> It is not really clear what you are trying to accomplish. If you move to the app dir and use a server component, this would not actually be more *secure* than `getServerSideProps`
Spectacled bearOP
But I have the pages router not the app dir
How should I do it on that?