Problem: I have to integrate with an existing repo that has been built via Next.js and I need to sign in to the app via an endpoint/url. I'm trying to leverage the signIn method of NextAuth.js but it keeps complaining about ReferenceError: window is not defined.
Testing Approach
- Add a
CredentialsProviderto[...nextauth].tsx:
const providers: Provider[] = [
..., //existing 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",
};
},
}),
];
- Under
/web/pages/api/auth/jwt.js
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: "test@example.com",
password: "myPassWord",
redirect: false,
});
res.status(201).json({ message: "Logged in user" });
}
This is simply setting up dummy endpoints/functions that simply go through the "success scenario" to see how this plays out. However, a GET/POST at /api/auth/jwt keeps failing with ReferenceError: window is not defined. I presume the signIn method is somehow dependent on the presence of a UI.
Question(s): How should I go about implementing an API-based sign-in? Or must I hack together an endpoint that actually returns an HTML, just so as to make this sign-in work?
