On the website https://code.google.com/apis/console I have registered my application, set up generated Client ID: and Client Secret to my a
This answer is same as this Mike's answer, and Jeff's answer, both sets redirect_uri
to postmessage
on client side. I want to add more about the server side, and also the special circumstance applying to this configuration.
create-react-app
version 2.1.5Summary: React --> request social auth "code" --> request jwt token to acquire "login" status in terms of your own backend server/database.
responseType="code"
to get an authorization code. (it's not token, not access token!)
react-google-login
mentioned above.{ "provider": "google-oauth2", "code": "your retrieved code here", "redirect_uri": "postmessage" }
REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI
, REST_SOCIAL_DOMAIN_FROM_ORIGIN
and REST_SOCIAL_OAUTH_REDIRECT_URI
in Django's settings.py
are unnecessary. (They are constants used by Django REST Social Auth) In short, you don't have to setup anything related to redirect url in Django. The "redirect_uri": "postmessage"
in React frontend suffice. This makes sense because the social auth work you have to do on your side is all Ajax-style POST request in frontend, not submitting any form whatsoever, so actually no redirection occur by default. That's why the redirect url becomes useless if you're using the code + JWT flow, and the server-side redirect url setting is not taking any effect.youremailprefix717e248c5b924d60
if your email is youremailprefix@example.com
. It appends some random string to make a unique username. This is the default behavior, I believe you can customize it and feel free to dig into their documentation.Authorization
header and send request to backend, Django backend will now recognize that as a login, i.e. authenticated user. Of course, if your token expire, you have to refresh it by making another request.Oh my goodness, I've spent more than 6 hours and finally got this right! I believe this is the 1st time I saw this postmessage
thing. Anyone working on a Django + DRF + JWT + Social Auth + React
combination will definitely crash into this. I can't believe none of the article out there mentions this except answers here. But I really hope this post can save you tons of time if you're using the Django + React stack.