Replies: 10 comments 12 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.
-
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