Skip to content

Commit 54e5711

Browse files
committed
Merge branch 'socialauth'
2 parents ba83a56 + 297959f commit 54e5711

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

django_school/classroom/views/classroom.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.shortcuts import redirect, render
22
from django.views.generic import TemplateView
3-
3+
from ..models import Student
44

55
class SignUpView(TemplateView):
66
template_name = 'registration/signup.html'
@@ -13,3 +13,13 @@ def home(request):
1313
else:
1414
return redirect('students:quiz_list')
1515
return render(request, 'classroom/home.html')
16+
17+
18+
def save_github_user(backend, user, response, *args, **kwargs):
19+
if backend.name == 'github':
20+
if not user.is_student:
21+
user.is_student = True
22+
user.save()
23+
student = Student.objects.create(user=user)
24+
# avatar_url = response.get('avatar_url')
25+
# print(user, response)

django_school/django_school/settings.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import os
14+
from decouple import config
1415

1516
from django.contrib.messages import constants as messages
1617

@@ -22,7 +23,7 @@
2223
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
2324

2425
# SECURITY WARNING: keep the secret key used in production secret!
25-
SECRET_KEY = 'd$pxg6fisc4iwzk&vz^s_d0lkf&k63l5a8f!obktw!jg#4zvp3'
26+
SECRET_KEY = config('SECRET_KEY')
2627

2728
# SECURITY WARNING: don't run with debug turned on in production!
2829
DEBUG = True
@@ -42,7 +43,7 @@
4243
'django.contrib.humanize',
4344

4445
'crispy_forms',
45-
46+
'social_django',
4647
'classroom',
4748
]
4849

@@ -54,6 +55,8 @@
5455
'django.contrib.auth.middleware.AuthenticationMiddleware',
5556
'django.contrib.messages.middleware.MessageMiddleware',
5657
'django.middleware.clickjacking.XFrameOptionsMiddleware',
58+
59+
'social_django.middleware.SocialAuthExceptionMiddleware',
5760
]
5861

5962
ROOT_URLCONF = 'django_school.urls'
@@ -71,6 +74,9 @@
7174
'django.template.context_processors.request',
7275
'django.contrib.auth.context_processors.auth',
7376
'django.contrib.messages.context_processors.messages',
77+
78+
'social_django.context_processors.backends', # <--
79+
'social_django.context_processors.login_redirect', # <--
7480
],
7581
},
7682
},
@@ -123,21 +129,39 @@
123129
]
124130

125131

126-
# Custom Django auth settings
132+
AUTHENTICATION_BACKENDS = [
133+
'social_core.backends.github.GithubOAuth2',
134+
# 'social_core.backends.twitter.TwitterOAuth',
135+
# 'social_core.backends.facebook.FacebookOAuth2',
136+
137+
'django.contrib.auth.backends.ModelBackend',
138+
]
139+
SOCIAL_AUTH_PIPELINE = (
140+
'social_core.pipeline.social_auth.social_details',
141+
'social_core.pipeline.social_auth.social_uid',
142+
'social_core.pipeline.social_auth.auth_allowed',
143+
'social_core.pipeline.social_auth.social_user',
144+
'social_core.pipeline.user.get_username',
145+
'social_core.pipeline.user.create_user',
146+
'classroom.views.classroom.save_github_user', # <--- set the path to the function
147+
'social_core.pipeline.social_auth.associate_user',
148+
'social_core.pipeline.social_auth.load_extra_data',
149+
'social_core.pipeline.user.user_details',
150+
)
151+
152+
SOCIAL_AUTH_GITHUB_KEY = config('SOCIAL_AUTH_GITHUB_KEY')
153+
SOCIAL_AUTH_GITHUB_SECRET = config('SOCIAL_AUTH_GITHUB_SECRET')
127154

155+
# Custom Django auth settings
128156
AUTH_USER_MODEL = 'classroom.User'
129157

130158
LOGIN_URL = 'login'
131-
132159
LOGOUT_URL = 'logout'
133-
134160
LOGIN_REDIRECT_URL = 'home'
135-
136161
LOGOUT_REDIRECT_URL = 'home'
137162

138163

139164
# Messages built-in framework
140-
141165
MESSAGE_TAGS = {
142166
messages.DEBUG: 'alert-secondary',
143167
messages.INFO: 'alert-info',
@@ -148,5 +172,4 @@
148172

149173

150174
# Third party apps configuration
151-
152175
CRISPY_TEMPLATE_PACK = 'bootstrap4'

django_school/django_school/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
urlpatterns = [
77
path('admin/', admin.site.urls),
88
path('', include('classroom.urls')),
9-
path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
9+
# path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
1010
path('accounts/', include('django.contrib.auth.urls')),
1111
path('accounts/signup/', classroom.SignUpView.as_view(), name='signup'),
1212
path('accounts/signup/student/', students.StudentSignUpView.as_view(), name='student_signup'),
1313
path('accounts/signup/teacher/', teachers.TeacherSignUpView.as_view(), name='teacher_signup'),
14+
path('oauth/', include('social_django.urls', namespace='social')), # <--
1415
]

django_school/templates/registration/login.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ <h2>Log in</h2>
2323
{{ form.password|as_crispy_field }}
2424
<button type="submit" class="btn btn-primary">Log in</button>
2525
</form>
26+
<br>
27+
<p><strong>-- OR --</strong></p>
28+
<a href="{% url 'social:begin' 'github' %}">Login with GitHub</a><br>
2629
</div>
2730
</div>
2831
{% endblock %}

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
Django==3.0.4
22
django-crispy-forms==1.9.0
3+
social-auth-app-django==3.1.0
4+
5+
python-decouple

0 commit comments

Comments
 (0)