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

Convert server side authentication to Next.js middleware #67

Open
elliotBraem opened this issue Aug 10, 2022 · 0 comments
Open

Convert server side authentication to Next.js middleware #67

elliotBraem opened this issue Aug 10, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@elliotBraem
Copy link
Member

elliotBraem commented Aug 10, 2022

In order to protect routes from unauthorized users, protected pages have this code snippet:

export const getServerSideProps = async (ctx) => {
  try {
    const admin = getFirebaseAdmin();
    const cookies = parseCookies(ctx);
    await admin.auth().verifyIdToken(cookies.__session);

    return {
      props: {},
    };
  } catch (err) {
    // either the `__session` cookie didn't exist
    // or token verification failed
    // either way: redirect to the login page
    return {
      redirect: {
        permanent: false,
        destination: "/login",
      },
      props: {},
    };
  }
};

This is duplicated on every page that is protected (almost all pages...)

Next.js 12 introduces middleware which should be able to replace this duplicate code and move it to its own _middleware.js file.

Note: Relevant YouTube snippet that inspired this issue.

The following routes should be protected:

  • /collect
  • /request
  • /organize
  • /profile
@elliotBraem elliotBraem added the enhancement New feature or request label Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

1 participant