Skip to content

Commit

Permalink
feat(social-login): Enable social login
Browse files Browse the repository at this point in the history
Currently, the only way a user gains access to the system is
if they register/login using the login and register form
which is inconvinient since the user has to remember their
credentials.

This commit integrates the ability to login using social
media accounts ie. twitter, facebook and google which
improves the UX.

[Delivers #157731629]
  • Loading branch information
King-Benx committed Jun 8, 2018
1 parent bca4f5a commit 139dadd
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ django-bower
invoke
requests
psycopg2-binary
coverage
whitenoise
gunicorn
dj-database-url==0.4.2
dj-static==0.0.6
social-auth-app-django

# REST API
djangorestframework>=3.2,<3.3
Expand All @@ -36,4 +36,3 @@ django-cors-headers
six
autopep8
flake8
psycopg2-binary
14 changes: 10 additions & 4 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('WG_NAME') or "test_wger",
'NAME': os.environ.get('WG_NAME') or 'test_wger',
'USER': os.environ.get('WG_USER') or 'postgres',
'HOST': os.environ.get('WG_HOST') or 'localhost',
'PASSWORD': os.environ.get('WG_PASSWORD') or '',
'PORT': os.environ.get('WG_PORT') or '',
'PORT': os.environ.get('WG_PORT') or '5432',
}
}

Expand All @@ -46,7 +46,7 @@
ALLOWED_HOSTS = '*'

# This might be a good idea if you setup memcached
# SESSION_ENGINE = "django.contrib.sessions.backends.cache"
#SESSION_ENGINE = "django.contrib.sessions.backends.cache"

# Configure a real backend in production
if DEBUG:
Expand All @@ -56,4 +56,10 @@
WGER_SETTINGS['EMAIL_FROM'] = 'wger Workout Manager <wger@example.com>'

# Your twitter handle, if you have one for this instance.
# WGER_SETTINGS['TWITTER'] = ''
#WGER_SETTINGS['TWITTER'] = ''

try:
from local_settings import *
except ImportError:
pass

4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
universal = 1

[pep8]
exclude = urls.py,tasks.py,*migrations*,*bower_components*
exclude = urls.py,tasks.py,*migrations*,*bower_components*,*env*,
max-line-length = 100
ignore = W503

# Flake8 configuration settings
[flake8]
exclude = .git,__pycache__,urls.py,tasks.py,generator.py,*migrations*,*bower_components*,*docs*,__*__,
exclude = .git,__pycache__,urls.py,tasks.py,generator.py,*migrations*,*bower_components*,*docs*,*env*,__*__,
max-line-length = 100
ignore = F401,W503
5 changes: 5 additions & 0 deletions wger/core/templates/user/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
{% trans 'Login' as submit_text %}
{% render_form_fields form submit_text %}
</form>
<hr/>
<h3 class="text text-primary text-center">Login/Register with social apps</h3>
<div class="row text-center">
<a href="{% url 'social:begin' 'facebook' %}"><i class="fa fa-4x fa-facebook"></i></a>&nbsp;&nbsp;<a href="{% url 'social:begin' 'twitter' %}"><i class="fa fa-4x fa-twitter text-info"></i></a>&nbsp;&nbsp;<a href="{% url 'social:begin' 'google-oauth2' %}"><i class="fa fa-4x fa-google text-danger"></i></a>
</div>
{% endblock %}


Expand Down
1 change: 1 addition & 0 deletions wger/exercises/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ class MuscleSerializer(serializers.ModelSerializer):
'''
class Meta:
model = Muscle

3 changes: 2 additions & 1 deletion wger/exercises/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
Expand Down Expand Up @@ -90,7 +91,6 @@ class ExercisesViewSet(viewsets.ReadOnlyModelViewSet):
def search(request):
'''
Searches for exercises.
This format is currently used by the exercise search autocompleter
'''
q = request.GET.get('term', None)
Expand Down Expand Up @@ -216,3 +216,4 @@ class MuscleViewSet(viewsets.ReadOnlyModelViewSet):
ordering_fields = '__all__'
filter_fields = ('name',
'is_front')

2 changes: 2 additions & 0 deletions wger/exercises/tests/test_exercise.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -552,3 +553,4 @@ def test_exercise_read_only(self):
response = client.post('/api/v2/exercises/', data=data)
# Test for method not allowed
self.assertEqual(response.status_code, 405)

32 changes: 30 additions & 2 deletions wger/settings_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))


#
# Application definition
#
SITE_ID = 1
ROOT_URLCONF = 'wger.urls'
WSGI_APPLICATION = 'wger.wsgi.application'

# twitter keys
SOCIAL_AUTH_TWITTER_KEY = os.environ.get('SOCIAL_AUTH_TWITTER_KEY')
SOCIAL_AUTH_TWITTER_SECRET = os.environ.get('SOCIAL_AUTH_TWITTER_SECRET')
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
# facebook keys
SOCIAL_AUTH_FACEBOOK_KEY = os.environ.get('SOCIAL_AUTH_FACEBOOK_KEY')
SOCIAL_AUTH_FACEBOOK_SECRET = os.environ.get('SOCIAL_AUTH_FACEBOOK_SECRET')
# google keys
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get('SOCIAL_AUTH_GOOGLE_OAUTH_KEY')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get('SOCIAL_AUTH_GOOGLE_OAUTH_SECRET')

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down Expand Up @@ -89,6 +101,9 @@

# django-bower for installing bower packages
'djangobower',

# social login
'social_django',
)

# added list of external libraries to be installed by bower
Expand Down Expand Up @@ -126,14 +141,22 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',


# social login
'social_django.middleware.SocialAuthExceptionMiddleware',

# Django mobile
'django_mobile.middleware.MobileDetectionMiddleware',
'django_mobile.middleware.SetFlavourMiddleware',
)

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'wger.utils.helpers.EmailAuthBackend'
'wger.utils.helpers.EmailAuthBackend',
# social login
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.google.GoogleOAuth2',
)

TEMPLATES = [
Expand All @@ -157,7 +180,11 @@
'django_mobile.context_processors.flavour',

# Breadcrumbs
'django.template.context_processors.request'
'django.template.context_processors.request',

# social login
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
'loaders': [
# Django mobile
Expand Down Expand Up @@ -385,3 +412,4 @@

if 'DATABASE_URL' in os.environ:
DATABASES = {'default': dj_database_url.config()}

2 changes: 2 additions & 0 deletions wger/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
url(r'config/', include('wger.config.urls', namespace='config', app_name='config')),
url(r'gym/', include('wger.gym.urls', namespace='gym', app_name='gym')),
url(r'email/', include('wger.email.urls', namespace='email')),
url(r'^oauth/', include('social_django.urls', namespace='social')),
url(r'^sitemap\.xml$',
sitemap,
{'sitemaps': sitemaps},
Expand Down Expand Up @@ -177,3 +178,4 @@
#
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

0 comments on commit 139dadd

Please sign in to comment.