Home > OS >  React hooks and getStaticProps
React hooks and getStaticProps

Time:01-02

I want to access hooks from getStaticProps to get the accessToken of the logged user which is saved in AuthContext. but this is impossible

I am using getStaticProps of Nextjs to pre-render pages, this is an example :

export async function getStaticProps() {
  const result = await axios.get("http://127.0.0.1:5000/track/landing-tracks-for-admin");
  return {
    props: {
      tracks: result.data,
    },
    revalidate: 5,
  };
}

the problem is that this request needs the user accesToken which I can get from useAuth

import useAuth from "../../hooks/useAuth";

but when I try to call it in getStaticProps like this : const auth = useAuth(); I get this error

React Hook "useAuth" is called in function "getStaticProps" that is neither a React function component nor a custom React Hook function

any other alternatives ?

CodePudding user response:

I guess you cannot authenticate the user like this. Please follow the documentation on Nextjs for authentication.

There is also an example repository for static page authentication using with-iron-session.

I suggest that you implement the user Authentication the nextjs way since you are using nextjs

  • Related