Skip to content

Commit 7e56051

Browse files
authored
Merge pull request #33 from ansuha/master
Settings changes for google based authentication
2 parents e2d1ff5 + 3d9b8d0 commit 7e56051

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ Django poll app is a full featured polling app. You have to register in this app
2929
<code>seeder.seed_all(30)</code>
3030
<p>Here 30 is a number of entry. You can use it as your own</p>
3131

32+
<h2>Obtaining OAuth Client ID for Google:</h2>
33+
34+
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:
35+
36+
1. **Go to the Google Cloud Console:**
37+
- Navigate to [Google Cloud Console](https://console.cloud.google.com/).
38+
- Sign in with your Google account.
39+
40+
2. **Create a new project:**
41+
- Click on the project dropdown menu at the top of the page.
42+
- Click on "New Project" and follow the prompts to create a new project.
43+
44+
3. **Enable the Google Identity service:**
45+
- In the Google Cloud Console, navigate to "APIs & Services" > "Dashboard."
46+
- Click on "Enable APIs and Services."
47+
- Search for "Google Identity" or "Google+ API" and enable it for your project.
48+
49+
4. **Create OAuth consent screen:**
50+
- In the Google Cloud Console, navigate to "APIs & Services" > "OAuth consent screen."
51+
- Fill in the required fields (like application name, user support email, etc.).
52+
- Add scopes (permissions) your application requires.
53+
- Save the consent screen information.
54+
55+
5. **Create OAuth credentials:**
56+
- In the Google Cloud Console, navigate to "APIs & Services" > "Credentials."
57+
- Click on "Create Credentials" > "OAuth client ID."
58+
- Select "Web application" as the application type.
59+
- Enter a name for your OAuth client.
60+
- Add authorized redirect URIs : `http://127.0.0.1:8000/complete/google-oauth2/`
61+
- Click "Create."
62+
63+
6. **Copy the client ID and client secret:**
64+
- Once the OAuth client is created, you'll see your client ID and client secret.
65+
- Copy these values and update following variables in settings.py
66+
67+
<code>SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id'
68+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret'</code>
69+
70+
For detailed instructions, refer to Google's documentation on [OAuth 2.0](https://developers.google.com/identity/protocols/oauth2).
71+
3272
<h2> To run the program in local server use the following command </h2>
3373
<code>python manage.py runserver</code>
3474

accounts/templates/accounts/login.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{% block content %}
44
<div class="vh-100 d-flex justify-content-center align-items-center p-5" id="login-content">
55
<div class="col-md-5 p-5 shadow-sm border rounded-5 border-primary bg-white">
6+
7+
<a href="{% url 'social:begin' 'google-oauth2' %}">Login with Google</a>
8+
69
<h2 class="text-center mb-4 text-primary">Login</h2>
710
{% if messages %}
811
<div class="messages">
@@ -38,4 +41,4 @@ <h2 class="text-center mb-4 text-primary">Login</h2>
3841
</div>
3942
</div>
4043
</div>
41-
{% endblock %}
44+
{% endblock %}

pollme/settings.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Application definition
3232

3333
INSTALLED_APPS = [
34+
'social_django',
3435
'django.contrib.admin',
3536
'django.contrib.auth',
3637
'django.contrib.contenttypes',
@@ -41,6 +42,11 @@
4142
'accounts.apps.AccountsConfig',
4243
]
4344

45+
AUTHENTICATION_BACKENDS = (
46+
'social_core.backends.google.GoogleOAuth2',
47+
'django.contrib.auth.backends.ModelBackend',
48+
)
49+
4450
MIDDLEWARE = [
4551
'django.middleware.security.SecurityMiddleware',
4652
'django.contrib.sessions.middleware.SessionMiddleware',
@@ -64,6 +70,8 @@
6470
'django.template.context_processors.request',
6571
'django.contrib.auth.context_processors.auth',
6672
'django.contrib.messages.context_processors.messages',
73+
'social_django.context_processors.backends',
74+
'social_django.context_processors.login_redirect',
6775
],
6876
},
6977
},
@@ -123,3 +131,8 @@
123131
STATICFILES_DIRS = [
124132
os.path.join(BASE_DIR, 'static')
125133
]
134+
135+
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id'
136+
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret'
137+
SOCIAL_AUTH_URL_NAMESPACE = 'social'
138+
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/polls/list/user/'

pollme/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
path('admin/', admin.site.urls),
2323
path('accounts/', include('accounts.urls', namespace="accounts")),
2424
path('polls/', include('polls.urls', namespace="polls")),
25+
path('', include('social_django.urls', namespace='social')),
2526
]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ asgiref==3.3.1
22
Django==3.1.14
33
pytz==2020.5
44
sqlparse==0.4.4
5+
social-auth-app-django>=0.4.0

0 commit comments

Comments
 (0)