I'm making a NextJs application with next-auth for the authentication part. Email Sign In is successfully implemented using next-auth's own default pages.
But now I would like to have a custom sign in page. I followed the documentation for this, and added
pages: { signIn: '/auth/signin' }
in my [...nextauth].js file. Then, I added the given Email Sign In code in pages/auth/signin.js.
But upon running yarn dev
, I get this module not found error:
error - ./pages/api/auth/signin.js:1:0
Module not found: Package path ./react is not exported from package C:\...\node_modules\next-auth (see exports field in C:\...\node_modules\next-auth\package.json)
> 1 | import { getCsrfToken } from "next-auth/react"
2 |
3 | export default function SignIn({ csrfToken }) {
4 | return (
Import trace for requested module:
https://nextjs.org/docs/messages/module-not-found
And I couldn't find any module named 'next-auth/react' in npm or yarn websites. Even in next-auth folder in node_modules, there is no 'react' named file...
How can I solve this? And am I doing anything wrong here?
NextAuth.js + React Query You can create your own session management solution using data fetching libraries like React Query or SWR. You can use the original implementation of @next-auth/react-query and look at the next-auth/react source code as a starting point.
NextAuth. js implements Access Tokens in sessions as a way to provide an identifier for client side operations that can be tracked by to a session, without exposing the Session Token itself (so that a Session cannot be hijacked by a third party script).
NextAuth. js is a complete open-source authentication solution for Next. js applications. It is designed from the ground up to support Next. js and Serverless.
I faced the same issue and realised the docs are for v4 where next-auth/react
is used.
You are probably on v3 where next-auth/client
is used instead.
To use the beta version, do:
➜ npm i next-auth@beta
I think it should be imported from client
and not react
try this : import { getCsrfToken } from "next-auth/client"
Also,
(just sharing an alternate solution), you need not define the custom pages in next auth. you can have your own login page and there just call next-auth's signin
method, by passing the type like email
or google
.
and if email, then pass the email as well. eg:
const handleSubmit = (event) => {
event.preventDefault();
signIn("email", { email, callbackUrl: `${process.env.VERCEL_URL}/` });
};
You can now run npm install next-auth
or yarn add next-auth
. This will update the version of next-auth to version 4 in which you import SessionProvider as follows (within _app.tsx) :
import { SessionProvider } from "next-auth/react"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With