Skip to content

Commit

Permalink
Merge pull request #33 from ansuha/master
Browse files Browse the repository at this point in the history
Settings changes for google based authentication
  • Loading branch information
devmahmud authored May 3, 2024
2 parents e2d1ff5 + 3d9b8d0 commit 7e56051
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ Django poll app is a full featured polling app. You have to register in this app
<code>seeder.seed_all(30)</code>
<p>Here 30 is a number of entry. You can use it as your own</p>

<h2>Obtaining OAuth Client ID for Google:</h2>

To use Google's OAuth authentication in your application, you need to obtain a client ID and client secret. Follow these steps to get your OAuth client ID for Google:

1. **Go to the Google Cloud Console:**
- Navigate to [Google Cloud Console](https://console.cloud.google.com/).
- Sign in with your Google account.

2. **Create a new project:**
- Click on the project dropdown menu at the top of the page.
- Click on "New Project" and follow the prompts to create a new project.

3. **Enable the Google Identity service:**
- In the Google Cloud Console, navigate to "APIs & Services" > "Dashboard."
- Click on "Enable APIs and Services."
- Search for "Google Identity" or "Google+ API" and enable it for your project.

4. **Create OAuth consent screen:**
- In the Google Cloud Console, navigate to "APIs & Services" > "OAuth consent screen."
- Fill in the required fields (like application name, user support email, etc.).
- Add scopes (permissions) your application requires.
- Save the consent screen information.

5. **Create OAuth credentials:**
- In the Google Cloud Console, navigate to "APIs & Services" > "Credentials."
- Click on "Create Credentials" > "OAuth client ID."
- Select "Web application" as the application type.
- Enter a name for your OAuth client.
- Add authorized redirect URIs : `http://127.0.0.1:8000/complete/google-oauth2/`
- Click "Create."

6. **Copy the client ID and client secret:**
- Once the OAuth client is created, you'll see your client ID and client secret.
- Copy these values and update following variables in settings.py

<code>SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret'</code>

For detailed instructions, refer to Google's documentation on [OAuth 2.0](https://developers.google.com/identity/protocols/oauth2).

<h2> To run the program in local server use the following command </h2>
<code>python manage.py runserver</code>

Expand Down
5 changes: 4 additions & 1 deletion accounts/templates/accounts/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{% block content %}
<div class="vh-100 d-flex justify-content-center align-items-center p-5" id="login-content">
<div class="col-md-5 p-5 shadow-sm border rounded-5 border-primary bg-white">

<a href="{% url 'social:begin' 'google-oauth2' %}">Login with Google</a>

<h2 class="text-center mb-4 text-primary">Login</h2>
{% if messages %}
<div class="messages">
Expand Down Expand Up @@ -38,4 +41,4 @@ <h2 class="text-center mb-4 text-primary">Login</h2>
</div>
</div>
</div>
{% endblock %}
{% endblock %}
13 changes: 13 additions & 0 deletions pollme/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# Application definition

INSTALLED_APPS = [
'social_django',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -41,6 +42,11 @@
'accounts.apps.AccountsConfig',
]

AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -64,6 +70,8 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
Expand Down Expand Up @@ -123,3 +131,8 @@
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret'
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/polls/list/user/'
1 change: 1 addition & 0 deletions pollme/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls', namespace="accounts")),
path('polls/', include('polls.urls', namespace="polls")),
path('', include('social_django.urls', namespace='social')),
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ asgiref==3.3.1
Django==3.1.14
pytz==2020.5
sqlparse==0.4.4
social-auth-app-django>=0.4.0

0 comments on commit 7e56051

Please sign in to comment.