<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: elite_developer</title>
    <description>The latest articles on DEV Community by elite_developer (@elite_dev).</description>
    <link>https://dev.to/elite_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2869456%2F63ebbc62-1e1b-4253-988c-0ee01f4609c7.jpeg</url>
      <title>DEV Community: elite_developer</title>
      <link>https://dev.to/elite_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elite_dev"/>
    <language>en</language>
    <item>
      <title>Integrate Google OAuth2 Social Authentication into your Django x React Web Application.</title>
      <dc:creator>elite_developer</dc:creator>
      <pubDate>Thu, 28 May 2026 00:16:04 +0000</pubDate>
      <link>https://dev.to/elite_dev/integrate-google-oauth2-social-authentication-into-your-django-x-react-web-application-56g6</link>
      <guid>https://dev.to/elite_dev/integrate-google-oauth2-social-authentication-into-your-django-x-react-web-application-56g6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A VS Code editor installed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More than basic understanding of Django and React&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A Custom or Pre built django user model already configured&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;set up virtual environment and pip install these packages&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 -m venv .venv

# start virtual environment
.venv/scripts/activate # windows command

# then install
pip install django django-allauth
pip install django-allauth dj-rest-auth djangorestframework-simplejwt


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now we Build&lt;/strong&gt;&lt;br&gt;
Inside your root directory, create a Django project like this:&lt;br&gt;
&lt;code&gt;startproject core .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Django will create a new directory called &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;core&lt;br&gt;
 in the current working directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, create an application called &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;authentication&lt;br&gt;
:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;py manage.py startapp authentication&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above command invocation will create an app directory called authentication at the same level as your project directory core. Proceed to include your new application in the list of INSTALLED_APPS section of the newly created settings.py file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# core/settings.py
INSTALLED_APPS =  [
    ...
    'authentication',
    ...
]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Django Configuration (settings.py)&lt;/strong&gt;&lt;br&gt;
Add the required apps and configure the authentication backends.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
    # ... standard apps
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'dj_rest_auth.registration',
]

SITE_ID = 1

REST_AUTH = {
    'USE_JWT': True, # Use JWT for modern React SPA authentication
}

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]

# Google settings
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': ['profile', 'email'],
        'AUTH_PARAMS': {'access_type': 'online'},
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set up an App in Google Developer Console
&lt;/h2&gt;

&lt;p&gt;Set up an App in &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Developer Console&lt;/a&gt;&lt;br&gt;
You need to set up an app in Google Console to enable users of your application authenticate with Google. The process involves two steps:&lt;/p&gt;

&lt;p&gt;Configuring an OAuth consent screen&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Setting up Credentials&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OAuth consent screen&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visit &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google developer console&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new project with a suitable name from the dropdown menu.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbybo5wxevldqo6m39923.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbybo5wxevldqo6m39923.png" alt="Image Image showing google dev console " width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select to use the project you have just created from the dropdown menu.&lt;br&gt;
In the displayed window, select OAuth consent screen and select the External option, then hit Create.Google developer console OAuth2 consent screen&lt;br&gt;
Fill in only the App information and Developer information details under Oauth2 consent screen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on Save and continue.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on Save and continue button for both Scopes and Test users forms without filling in anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Confirm your Oauth2 details in the Summary section then click on Back to dashboard.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set up Credentials&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate Credentials under APIs and Services&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click on Create Credentials and select OAuth Client ID from the dropdown options.The credentials screen in Google developer console&lt;br&gt;
Select Web application for the Application type field. You could use the default name under the Name field or provide a suitable name.&lt;br&gt;
Select ADD URI under Authorized JavaScript Origins to add a Uniform Resource Identifier (URI).&lt;/p&gt;

&lt;p&gt;Info: Adding a URI ensures only permitted domains can send requests to your OAuth2 app from a browser. Since you will be using your react app in development, add this local host URI:&lt;br&gt;
&lt;code&gt;http://localhost:5173/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Select ADD URI under Authorized Redirect URIs and add the following URI:&lt;br&gt;
&lt;code&gt;http://localhost:5173/dashboard&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Info:This is the callback URL where Google will redirect to after a user grants or denies your application permission to authenticate with their Google account.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hit the Create button and wait for a few seconds for your app to be ready. Your new app will generate a Client ID and Client secret. In the resulting window, download the JSON for these details or copy and paste the Client ID and Client secret somewhere. You will need these details in a few.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Setup the API Bridge (views.py)&lt;/strong&gt;&lt;br&gt;
This is where you define the bridge that links Google’s response to your Django user system. Create this in your app's views.py.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from dj_rest_auth.registration.views import SocialLoginView
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    # client_class is only needed if you are using specific OAuth2 flows
    client_class = OAuth2Client





&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Define the URL (urls.py)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# urls.py
from django.urls import path
from .views import GoogleLogin

urlpatterns = [
    path('api/auth/google/', GoogleLogin.as_view(), name='google_login'),
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Frontend (React)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. npm install&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;npm install @react-oauth/google axios&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Wrap your App&lt;/strong&gt;&lt;br&gt;
Wrap your main component so the Google SDK is available globally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js or App.js
import { GoogleOAuthProvider } from '@react-oauth/google';

export default function App() {
  return (
    &amp;lt;GoogleOAuthProvider clientId="YOUR_CLIENT_ID_FROM_GOOGLE_CLOUD.apps.googleusercontent.com"&amp;gt;
      &amp;lt;Login /&amp;gt;
    &amp;lt;/GoogleOAuthProvider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. The Login Logic&lt;/strong&gt;&lt;br&gt;
Use the useGoogleLogin hook. It handles the pop-up and gives you the access_token to send to your Django API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Login.jsx
import { useGoogleLogin } from '@react-oauth/google';
import axios from 'axios';

export default function Login() {
  const googleLogin = useGoogleLogin({
    onSuccess: async (tokenResponse) =&amp;gt; {
      try {
        // Send the token to your Django endpoint
        const response = await axios.post('http://localhost:8000/api/auth/google/', {
          access_token: tokenResponse.access_token,
        });

        // The response contains your JWTs
        console.log("Success:", response.data);
        localStorage.setItem('access_token', response.data.access_token);
      } catch (error) {
        console.error("Auth failed:", error);
      }
    },
    onError: (error) =&amp;gt; console.log("Login Failed", error),
  });

  return &amp;lt;button onClick={() =&amp;gt; googleLogin()}&amp;gt;Sign in with Google&amp;lt;/button&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final Step - Admin Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you skip this, it will never work.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run py manage.py makemigrations and py manage.py migrate.&lt;/li&gt;
&lt;li&gt;Start your server and log in to /admin.&lt;/li&gt;
&lt;li&gt;Go to Social Applications.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Add.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provider: Google.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client ID &amp;amp; Secret: Paste these from your Google Cloud Console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sites: Move example.com to the "Chosen" box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>django</category>
      <category>oauth</category>
    </item>
  </channel>
</rss>
