#Problem in pulling drf api data in react.ts

14 messages · Page 1 of 1 (latest)

uncut rune
#

I am trying to check the user is logged in or not, so i am sending a get request to drf and i am using axios to fetch it, i followed a tutorial but it doesn't works, screenshot has Error, react.ts axios usage, drf view for it, i put corsheader in installed app and added it into middleware as well, i also added the react link in cors allowed host

views.py

class UserView(APIView):
    permission_classes = (permissions.IsAuthenticated, )
    authentication_classes = (SessionAuthentication, )

    def get(self, request):
        serializer = UserSerializer(request.user)
        return Response({"user": serializer.data}, status=status.HTTP_200_OK)```


**urls.py**
```urlpatterns = [
    path("api/botpanel/register/", UserRegister.as_view(), name="bot_panel_register_user"),
    path("api/botpanel/login/", UserLogin.as_view(), name="bot_panel_login_user"),
    path("api/botpanel/logout/", UserLogout.as_view(), name="bot_panel_logout_user"),
    path("api/botpanel/user/", UserView.as_view(), name="bot_panel_user"),
]```


**Settings.py**
```CORS_ALLOWED_ORIGINS = [
    'http://localhost:5173',  # Add the origin of your React app
]

# Optional: Allow credentials (cookies, authorization headers, etc.) to be sent
CORS_ALLOW_CREDENTIALS = True

CORS_TRUSTED_ORIGINS = [
    'http://localhost:5173',  # Add the origin of your React app
]

CSRF_COOKIE_HTTPONLY = False

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'```
indigo remnant
#

From what I can get from this, your session is active but you don't have permissions. You might want to clear the cache and try again

drifting carbon
opaque stone
#

Your API shows that the view is Authenticated. That means you need to authenticate before getting user data

uncut rune
#

🤔

#

so it is generating

#

csrftoken and session id in Application

#

but logout not working 🤔

#

Logout Function

        e.preventDefault();
        client.post(
            "/api/botpanel/logout/",
            {withCredentials: true}
        ).then(function(res) {
            console.log(res)
            setCurrentUser(false)
        })
    }```
#

wokring perfectly directly!

#

but with react, not

#

or it is working maybe? but the session id and csrf token are still there

uncut rune