Issue: I am facing a challenge integrating with an existing repository that was created using Next.js. The task at hand is to enable users to sign in to the application through a specific endpoint or URL. To achieve this, I have been attempting to utilize the signIn
function within NextAuth.js, but it consistently generates an error stating
ReferenceError: window is not defined
.
Testing Strategy
- Include a
CredentialsProvider
in the[...nextauth].tsx
file:
const providers: Provider[] = [
..., //exisiting providers
CredentialsProvider({
id: "jwt_auth",
name: "JWT-Auth",
credentials: {
email: { label: "Username", type: "text"},
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
return {
id: "usr123",
username: "testUser",
};
},
}),
];
- In the
/web/pages/api/auth/jwt.js
file
import { NextApiRequest, NextApiResponse } from "next";
import { signIn } from "next-auth/react";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await signIn<"credentials">("jwt_auth", {
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b3f2e383f0b2e332a263b272e65282426">[email protected]</a>",
password: "myPassWord",
redirect: false,
});
res.status(201).json({ message: "Logged in user" });
}
The current setup involves creating mock endpoints/functions that simulate a successful scenario to observe how the process unfolds. Despite these efforts, when sending GET/POST requests to /api/auth/jwt
, an error is triggered indicating
ReferenceError: window is not defined
. This suggests that the functionality of the signIn
method may rely on having a user interface.
Inquiry(s): What would be the best approach for implementing API-based sign-in functionality? Is it necessary to develop a workaround by crafting an endpoint that returns HTML content in order to facilitate the sign-in process?