Reputation: 31
I'm rewriting my company's whole React application onto NextJS 13.4.8 using app/ directory. We are using Auth0 as an auth provider (no plans for NextAuth) and from the documentation of the nextjs-auth
with app router (currently in beta, https://github.com/auth0/nextjs-auth0/tree/beta#app-router) I assume it should work... somehow. As of now, it is a plain NextJS app so it's an open sandbox.
The problem is that the app itself compiles but when going to the browser I'm having the following error:
- error TypeError: Cannot read properties of undefined (reading 'auth0')
at eval (webpack-internal:///(sc_server)/./node_modules/.pnpm/@[email protected][email protected]/node_modules/@auth0/nextjs-auth0/dist/handlers/auth.js:49:47)
at step (webpack-internal:///(sc_server)/./node_modules/.pnpm/[email protected]/node_modules/tslib/tslib.es6.mjs:260:23)
at Object.eval [as next] (webpack-internal:///(sc_server)/./node_modules/.pnpm/[email protected]/node_modules/tslib/tslib.es6.mjs:201:20)
at eval (webpack-internal:///(sc_server)/./node_modules/.pnpm/[email protected]/node_modules/tslib/tslib.es6.mjs:179:71)
at new Promise (<anonymous>)
I've done the following (with the use of docs on their gh):
app/api/auth/[auth0]/route.ts
// app/api/auth/[auth0]/route.ts
import { handleAuth } from '@auth0/nextjs-auth0';
export const GET = handleAuth();
RootLayout
with UserProvider
from @auth0/nextjs-auth0/client
// src/app/layout.tsx
import './globals.css';
import { UserProvider } from '@auth0/nextjs-auth0/client';
export const metadata = {
...
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<UserProvider>
<html lang='en'>
<body>{children}</body>
</html>
</UserProvider>
);
}
login
page I'm returning <a href='/api/auth/login'
- 'use client'
is present there// src/app/login/page.tsx
'use client'
import { useUser } from '@auth0/nextjs-auth0/client';
...
export default function Login() {
...
const { user, error, isLoading } = useUser();
...
return (
<a href='/api/auth/login'>
<Button size='lg'>
Login
</Button>
</a>
)
}
.env.local
there are: AUTH0_SECRET
, AUTH0_BASE_URL
, AUTH0_ISSUER_BASE_URL
, AUTH0_CLIENT_ID
and AUTH0_CLIENT_SECRET
taken from the Auth0 dashboard - this data is the same as in the old React appIs there something wrong with my solution, or there is a bug in this integration, or maybe there is some other problem that you see here?
Upvotes: 3
Views: 1518
Reputation: 979
I'm pretty certain your issue is/was that you are using import { UserProvider } from '@auth0/nextjs-auth0/client';
(e.g.; the client-side package) from your src/app/layout.tsx
file, which is not, or at least from the provided code does not seem to be, flagged as use client
, and hence is on the server-side of the setup.
You can fix it by using the UserProvider
in a client side component (possibly just a child of your layout.tsx
but marked as use client
).
Upvotes: 0
Reputation: 1287
make the route:
app/api/auth/route.ts
not
app/api/auth/[auth0]/route.ts
Upvotes: 0
Reputation: 39
I had the same problem and I solved it by upgrading my auth0 @auth0/nextjs-auth0 to 3.0.0-beta.3
Try this
npm i @auth0/[email protected]
https://www.npmjs.com/package/@auth0/nextjs-auth0?activeTab=versions
Upvotes: 0