0

I have my API set up using SessionAuthentication. Once a user logs in, I redirect them to their profile page in React. Once they are redirected to their profile page, I want to make a REST call to retrieve their profile data and insert it in the proper location on the page. I see a couple ways I can do this:

  1. When a user logs in, put their User ID into the Response object (DRF) and then store that in the client somewhere (Redux store, session storage, or local storage). Then when they are redirected to the login page, make a REST call to /users/users_id.
  2. With Django sessions the logged in user is automatically tied to each request. So do I even need to follow Rest here? I can make a call to /users, and if the user is authenticated, return their data.

I would appreciate any help with this. Thank you.

2
  • User logs in with Django? Commented Mar 29, 2020 at 20:30
  • @jTiKey But once they get redirected to their profile page, don't I need to make another call to the server to get the data? This is the part I am confused on. What endpoint do I hit? Commented Mar 29, 2020 at 22:23

1 Answer 1

1

With SessionAuthentication, after a successful login, the browser saves a sessionId cookie for that domain (or ip:port) automatically. Sending a request will send that cookie from the same domain no matter with Django or React, and authenticate the user, making your request.user a user.

You can check for the cookie when you inspect the page -> Application -> Cookies -> Your domain -> sessionId

Basically, you can login via Django and it will login you with React as well. No need to store anything manually. Just use the same domain for both.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that definitely helps. But where do I sent the request to? Something like 127.0.0.1/session/user? For example, if after the user logs in, I redirect them to their profile page. Would I sent a request to something like 127.0.0.1/sessions/user, which will make sure the user is authenticated and then return the user object to React?
Yes, to your django view that returns the request.user data in JSON.
Thank you. I was just confused because I wanted to keep it restful and do something like 127.0.0.1/users/, but since the proper way restful url would be /users/user_id, I wasn't sure how to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.