next-password-protect icon indicating copy to clipboard operation
next-password-protect copied to clipboard

getInitialProps from _app are lost when using withPasswordProtect()

Open specialfae opened this issue 3 years ago • 2 comments

Describe the bug Any props set within custom _app component are lost when exporting withPasswordProtect()

To Reproduce Steps to reproduce the behaviour:

  1. Setup a custom _app file with getInitialProps setting a value.
  2. Export using withPasswordProtect()
  3. Login successfully
  4. Console log _app props on page and they're undefined
  5. Leave everything as is except remove withPasswordProtect() and your props will re-appear on the page

Expected behavior Props from _app shouldn't be overwritten or lost.

Stack Node fermium, Next 12, next-password-protect 1.7

specialfae avatar Jun 29 '22 04:06 specialfae

+1

vildantursic avatar Jul 30 '22 07:07 vildantursic

You should be able to attach getInitialProps to the instance returned from the HOC instead. See this modified example from the readme:

import { withPasswordProtect } from "next-password-protect";

// Before: export default App;
const ProtectedApp = process.env.PASSWORD_PROTECT
  ? withPasswordProtect(App, {
    // Options go here (optional)
    loginApiUrl: "/login",
  })
  : App;

// Set getInitialProps on HOC instance
ProtectedApp.getInitialProps = () => {
  //
};

export default ProtectedApp;

vf-telwing avatar Mar 27 '24 18:03 vf-telwing