Which SDK this is regarding: nextjs-auth0
SDK Version: 1.9.1
Hey there! Working on converting our SPA to a MPA with NextJS. We have a single app that allows multiple tenants to connect to it. My custom initAuth0
function queries for the tenant information from our database to populate the required parameters.
export default async (req: NextApiRequest) => {
try {
const auth0Config = await appSyncClient.query<any>({
query: GET_AUTH0_CONFIG_QUERY,
});
return initAuth0({
...auth0Config
});
} catch (e) {
console.log('Error initilizing Auth0 client');
}
};
This works fine so far, but I need to send a request with every version of the instance (I think?) in order to grab the configuration. This all works fine except I’m having a hard time with withPageAuthRequired
.
withPageAuthRequired(DashboardPage)
doesnt work because I need to send the req
to the initAuth0
instance. I tried
export const getServerSideProps = async req => {
const auth = await auth0(req);
return auth?.withPageAuthRequired();
};
as well but getServerSideProps expects and object to be returned. Any hints here?
Turns out you can just use the SDK’s version, as initAuth0
is only for server side methods according to this issue:
opened 01:05PM - 12 Nov 21 UTC
closed 10:42AM - 15 Nov 21 UTC
question
### Description
Could you provide a solution for that scenario when we want t… o use the `withPageAuthRequired()` on CSR, but with an own instance? We use AWS secret manager to get the secrets for Auth0.
Following [your guidance](https://github.com/auth0/nextjs-auth0/issues/376#issuecomment-833488401) and the official Auth0 example from [here](https://github.com/auth0/nextjs-auth0/blob/v1.6.1/EXAMPLES.md#protecting-a-client-side-rendered-csr-page), we try to get this working with a code like this:
```
import {auth0} from '../../../utils/auth0';
const AuthRequiredCSRExample = props => {
// stuff...
return (
<>
<p>more stuff...</p>
</>
);
};
const instance = await auth0();
export default instance.withPageAuthRequired(AuthRequiredCSRExample);
```
In the `next.config.js` I had to enable the top-level-await feature:
```
config.experiments = {
...config.experiments,
topLevelAwait: true
};
```
But now I got an error message on the page which says:
![image](https://user-images.githubusercontent.com/1906302/140893645-23c6dbb1-a584-41b7-b461-b0402b44452f.png)
It looks like if we have to get the secrets from somewhere (e.g. AWS secret manager), we lose the ability to use this CSR `withPageAuthRequired()` feature. But correct me, please if I'm wrong!
Any help would be appreciated!
### Environment
- Version of this library used: 1.6.1
- Version of the platform or framework used, if applicable: Next.js 12.0.2
So, closing
1 Like