Skip to content

Centralise repeated session logic #90

Description

@oliverjam

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>
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions