Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralise repeated session logic #90

Open
oliverjam opened this issue Nov 26, 2021 · 0 comments
Open

Centralise repeated session logic #90

oliverjam opened this issue Nov 26, 2021 · 0 comments

Comments

@oliverjam
Copy link

const [session, setSession] = useState(null);
useEffect(() => {
setSession(supabase.auth.session());
supabase.auth.onAuthStateChange((_event, session) => {
setSession(session);
});
}, []);

You've got this identical bit of code for handling Supabase auth sessions in quite a few pages. Since it's likely that every page needs to know if you're logged in or not, you could put this into pages/_app.js. This components sits "above" all other pages, so any code in here will run for every page. You could then pass the session down as a prop to the pages:

// pages/_app.js

function MyApp({ Component, pageProps }) {
  const [session, setSession] = useState(null);
  useEffect(() => {
    setSession(supabase.auth.session());
    supabase.auth.onAuthStateChange((_event, session) => {
      setSession(session);
    });
  }, []);
  return (
    <Layout>
      <Component {...pageProps} session={session} />
    </Layout>
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant