Replies: 14 comments 16 replies
-
Is the 401 the normal response when wrong credentials are used and if so, why can I not catch it in my try-catch block as I do not want it to be displayed in the client's console? |
Beta Was this translation helpful? Give feedback.
-
I really need your help. I cannot go on like this. :( |
Beta Was this translation helpful? Give feedback.
-
Hi @JWebGit
|
Beta Was this translation helpful? Give feedback.
-
Another scenario where this can happen is bad configuration. Your Just leaving this here. It might help someone some day. |
Beta Was this translation helpful? Give feedback.
-
@kevinveiga solution above does work on me so far. Thanks |
Beta Was this translation helpful? Give feedback.
-
Same here too, any solution |
Beta Was this translation helpful? Give feedback.
-
I have same... Is there a solution? :C |
Beta Was this translation helpful? Give feedback.
-
downgrade nextjs version to 13.4.12 it works for me |
Beta Was this translation helpful? Give feedback.
-
I encountered the same problem when dockerizing. I'm using next js as the frontend and django as the backend api, when I started the provider call locally the authentication was successful when I started it in the docherized context I got the 401. Libs
The problem in my case was the url, using docker I was putting to the domain address The solution for me was to separate the 2 addresses using the domain on the client side, on the server side I use the docker container pointer Part of the docker compose frontend:
image: frontend-server
build:
context: ../../
dockerfile: ./docker/docker/frontend.Dockerfile
env_file:
- ".env"
restart: always
networks:
- app-network
ports:
- "3000:3000"
api:
image: api-server
build:
context: ../../
dockerfile: ./docker/docker/api.Dockerfile
args:
COLLECTSTATIC: "true"
env_file:
- ".env"
restart: always
command: poetry run gunicorn myapp.wsgi:application -b 0.0.0.0:8000 --workers 4 --timeout 120 --log-level info
depends_on:
db:
condition: service_healthy
migrations:
condition: service_completed_successfully
networks:
- app-network
ports:
- "8000:8000"
volumes:
- static:/app/static Env file included# API Server
API_DEBUG_MODE=False
API_SECRET_KEY="qwerty"
API_CORS_ALLOWED_ORIGINS="http://localhost,http://frontend:3000"
API_ALLOWED_HOSTS="*"
# Frontend Server
FRONTEND_API_URL="http://localhost/api_server"
FRONTEND_BACK_API_URL="http://api:8000/api_server"
NEXTAUTH_SECRET="qwerty"
NEXTAUTH_URL=http://localhost/api/auth
ConclusionsIn my case this problem cost me several hours of development and all due to the lack of a detailed log. I hope the solution or debugging process can be useful to someone |
Beta Was this translation helpful? Give feedback.
-
Try adding the custom error to check where you are going wrong, as I am trying to log in with the wrong password and when I throw custom errors I get the password not match error. Worked for me. async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password) {
throw new Error("Invalid credentials");
}
const existingUser = await db.user.findUnique({
where: { email: credentials?.email },
});
if (!existingUser) {
throw new Error(`User ${credentials?.email} not found`);
}
const passwordMatch = await compare(
credentials?.password,
existingUser.password
);
if (!passwordMatch) {
throw new Error(`User ${credentials?.email} not macth`);
}
return {
id: `${existingUser.id}`,
username: existingUser.username,
email: existingUser.email,
};
}, |
Beta Was this translation helpful? Give feedback.
-
A fix for MongoDB, I know this is a Prisma thread, but for anyone who faces this issue in MongoDB, simply ensure you are doing a connection request to MongoDB using try {
await connectMongo();
const user = await User.findOne({ email: credentials.email });
...
} |
Beta Was this translation helpful? Give feedback.
-
Likely SolutionA bit late on the reply, slight solution which may work out is to make use of the connect request first using try {
const prisma = new PrismaClient()
await prisma.$connect()
const allUsers = await prisma.user.findMany()
...
} ExplainationThis likely happens because in localhost or production, a client could not be connected to db according to the connection docs which states that Thus making it the case that it is likely not connected because you never made the first query such as post request (that happens during signup). Extra stuff belowShort good experiment you can do to test this is that, it will work when:
But it won't work if:
|
Beta Was this translation helpful? Give feedback.
-
For anyone encountering this issue when deploying to Vercel, try disabling Vercel's authentication feature. After I disabled it, everything worked as expected. |
Beta Was this translation helpful? Give feedback.
-
Question 💬
I am using the NextAuth.js credentials provider for the log in procedure. When logging in on my custom page I am catching the errors in a try-catch block and set the error state accordingly, but I do not display the error state anywhere yet. Even though I am catching the errors, a 401 unauthorized gets thrown when trying to log in. I am using wrong credentials, thus expecting an error called CredentialsSignin which I am getting, but additionally I am getting the 401 every time.
Here the code of my custom log in page:
Here the code of my [...nextauth].ts API page:
How to reproduce ☕️
Try to login using await signIn() within a try catch block
Open the console in the browser
Notice a 401 error that was not catched
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions