djoser: `State could not be found in server-side session data.` with React Frontend.
Hey everyone.
I am facing this State could not be found in server-side session data. issue with my app. I am pretty sure that I have configured everything properly since everything is working fine in the Postman and I am able to create user from the postman.
For the frontend, I am using nextjs. Making requests with code and state always comes back with "State could not be found in server-side session data." no matter what. Not sure where exactly is this coming from?
The fact that it is working with Postman might be because of the cookies sessionid and csrftoken which I am not very sure about.
If any mod can please help regarding this, would be very helpful.
Thanks
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 15
- Comments: 23
Can’t get this to work either using Postman or NuxtJS. Getting the same error. Not sure what alternatives there are if one still wishes to keep Djoser… any pointers? Did you end up still using Djoser but adding dj-rest-auth to it? If so, I would love to see how you implemented it. It really sucks because this turned out to be really difficult to debug. @mabdullahadeel
Hi, I was facing the same issue but I was able to solve it and was able to get the JWT Access Token and Refresh Token for Google Login without replacing DJoser.
I have my Front-end in React JS and Backend in DJango (DRF, DJoser, DJoser Social Login, drf_social_oauth2, social_django).
Problem: “non_field_errors”: [“Invalid state has been provided.”], I was getting this response when I performed POST request to _http://127.0.0.1:8000/auth/o/google-oauth2/?state=[state]&code=[code]_
Solution: The problem is probably with session storage. The following steps helped me solve it.
1. Add this to the React App where you are making HTTP/HTTPS Requests:
Source: _https://stackoverflow.com/questions/57305141/react-django-rest-framework-session-is-not-persisting-working/57316231#57316231_
2. In your DJango settings.py, set the following settings:
CORS_ALLOW_HEADERS = [“*”], this stopped working for some unknown reason. I had to specify the headers I was using.
Source: _https://stackoverflow.com/questions/57305141/react-django-rest-framework-session-is-not-persisting-working/57316231#57316231_
3. In addition, make sure “corsheaders.middleware.CorsMiddleware” is the first thing in the middleware.
I am not sure whether this helped in fixing the issue or not but I made this change which might have also played some role in fixing this issue.
Source: GitHub Copilot
GitHub Copilot:
4. Change your React App host:
This is the major change which helped in solving the problem.
My React App was being hosted at: localhost:3000/ and DJango at 127.0.0.1:8000/
I changed hosting of my React App from localhost:3000 -> 127.0.0.1:3000
So for example I was making my Google Login Request from 127.0.0.1/login-register, the route looked like: 127.0.0.1:3000/login-register -> Google Login Screen -> (Redirect) 127.0.0.1:3000/login-register
In my case I get redirected to 127.0.0.1:3000/login-register, so make sure the redirect uri is also changed to 127.0.0.1
My request looked like: Was
axios.get("http://127.0.0.1:8000/auth/o/google-oauth2/?redirect_uri=http://localhost:3000/login-register");Nowaxios.get("http://127.0.0.1:8000/auth/o/google-oauth2/?redirect_uri=http://127.0.0.1:3000/login-register");Don’t forget to add or change your base url from localhost:3000 -> 127.0.0.1:3000 in Authorized redirect URIs at Google Cloud -> Credentials.
Source: _https://stackoverflow.com/questions/57305141/react-django-rest-framework-session-is-not-persisting-working/57316231#57316231_
After applying the following changes, I was able to login with google and get Access and Refresh Tokens.
NOTE: I spent good time at fixing this issue. These are the changes which helped me in properly implementing Google Social Login. I hope these steps also help you fix this issue if you are facing. I have also attached screenshot of successful creation of User in the database. I have tried my best to share everything that has helped me.
User created:
Getting the Access Token Refresh Token and Logged In: