Custom Express server seemingly makes Next-Auth call wrong URL? #5051
-
Hello, Next-Auth community! I'm having an issue with Next-Auth. I've set the NEXTAUTH_URL to the correct (deployment) URL, which is running on port 443. (I've replaced the url with This was working fine, however after I implemented a custom Express server in place of the default Next I get this error:
All pages/routes work as expected from the browser. (But are obviously broken by the fact that the client can't get a session) I can call my APIs manually like Since this started happening when I replaced the server with a custom Express server I assume that must be the catalyst, but the routing seems to work fine otherwise. Any ideas? My custom server looks like this (partial): const https = require("https"); //TLS
const express = require("express"); //Express webserver
const next = require("next");
const { parse } = require("url");
const nextApp = next({ dev: true, hostname: "localhost", port: port });
const handle = nextApp.getRequestHandler();
const expressApp = express();
const port = 443;
async function StartExpress() {
await nextApp.prepare();
// Custom routes go here... eventually
// Hand all other routes to Next
expressApp.all("*", (req, res) => {
const parsedUrl = parse(req.url, true);
return handle(req, res, parsedUrl);
});
console.log(process.env.NEXTAUTH_URL); // https://example.com - seems to work as intended
const server = https.createServer(httpsOptions, expressApp);
server.listen(port, (err) => {
if (err) throw err;
console.log("Running on port " + server.address().port);
});
}
StartExpress(); Also, it seems something is calling the appropriate endpoint, if I monitor the server I do get a call to I assume my blunder lies somewhere in wrapping the next routing in express, however I just can't figure it out. Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As per usual I figured this out just after formulating this question. It turned out I was importing next-auth in the server, which is not supported and was accidental. Probably caused by overly eager auto-complete.
|
Beta Was this translation helpful? Give feedback.
As per usual I figured this out just after formulating this question. It turned out I was importing next-auth in the server, which is not supported and was accidental. Probably caused by overly eager auto-complete.
const { getSession } = require("next-auth/react")