Redirects to sign in page when trying to sign in
Question 💬
I have cloned the example repository and deployed it to AWS using the serverless-http library in a lambda function. I have added the CredentialsProvider as shown here. To deploy I run the build.sh script and deploy a function with the .dist directory and the server.js being the handler.
server.ts:
import { NextConfig } from "next";
import NextServer from "next/dist/server/next-server";
import serverless from "serverless-http";
// @ts-ignore
import { config } from "./.next/required-server-files.json";
const nextServer = new NextServer({
hostname: "localhost",
port: 3000,
dir: __dirname,
dev: false,
conf: {
...(config as NextConfig),
},
});
export const handler = serverless(nextServer.getRequestHandler(), {
binary: ["*/*"],
provider: "aws"
});
build.sh:
#!/bin/bash
BUILD_FOLDER=.dist
yarn build
rm -rf $BUILD_FOLDER
mv .next/standalone/ $BUILD_FOLDER/
cp -r .next/static $BUILD_FOLDER/.next
rm $BUILD_FOLDER/server.js
cp -r next.config.js $BUILD_FOLDER/
cp -r node_modules/serverless-http $BUILD_FOLDER/node_modules/serverless-http
tsc server.ts --outDir .dist --esModuleInterop true
cp -r public $BUILD_FOLDER/
Everything works as expected in terms of NextJs and protection of pages, thus I suspect it has nothing to do with the serverless setup (but I might be wrong). I'm more than happy to share the whole deployment setup, but since I get redirected around just fine, I suspect it is a next-auth related problem.
I have tried debugging the problem for a while now, but I am unable to find the cause. The closest I have gotten is the signIn() method which makes a fetch call to /api/auth/callback/credentials, but instead of returning JSON it returns HTML (due to the redirect), causing a parse error.
How to reproduce ☕️
I am unable to reproduce this locally, only when deployed.
- Click the blue "sign in" button at the top
- Get redirected to
/api/auth/signin - Click "Sign in with credentials"
- Get redirected to
/api/auth/callback/credentials - Get redirected back to
/api/auth/signin?csrf=truewith a 302 status - No session is created.
The same goes for any of the other default providers.
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Ran into this yesterday when deploying to AWS lambda for prod. Did you ever figure this out, spent the better part of a day on this already.
Ran into this yesterday when deploying to AWS lambda for prod. Did you ever figure this out, spent the better part of a day on this already.
Sadly no. Ended up going back to deploying using a Fargate EC2 instance.
Thanks for the update, I will dig into this more over the weekend if I get time. Good to hear I can fall back to EC2 though.
Hello ... I was able to have it working following this thread https://github.com/dougmoscrop/serverless-http/issues/227
In case it helps, In my case, a call to getServerSession in getServerSideProps was generating new csrfs, resulting in a mismatch between the csrf token in the form and the one set in the cookie.
Removingi this call fixed the issue
So, does this mean getServerSession in Next.js' getServerSideProps is broken and should not be used? Seems maintainers are working on unstable_getServerSession but that looks, well, unstable...
It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!
To keep things tidy, we are closing this issue for now. If you think your issue is still relevant, leave a comment and we might reopen it. Thanks!
Just about went mad trying to troubleshoot why next-auth was behaving differently deployed to a lambda behind an ALB versus locally. Needed to sign-in twice and logout twice for it to work. Come to find out there's this obscure attribute on the target group for lambdas:
Multi value headers Allows request and response headers exchanged between the load balancer and the Lambda function to use arrays. Otherwise, the load balancer uses the last value it receives.
Toggling this on fixed it for me. Turns out the client was only getting a single set-cookie header per response, which apparently took 2 requests each time.