This app is basic implementation of python-social-auth Python Social Auth is an easy to setup social authentication/registration mechanism with support for several frameworks and auth providers.
In this app basic implementation of social authentication with Google OAuth2 is created
Project documentation is available at http://python-social-auth.readthedocs.org/.
- create basic django applcation.
- install python-social-auth package.
$ pip install python-social-auth
- Add following in settings.py
To create social auth keys please refer to: https://developers.google.com/identity/sign-in/web/sign-in
# Add social_django in installed apps settings
INSTALLED_APPS = [
.....
'social_django',
.....
]
# add google auth backend and django default backend
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
# create app in google console and add api keys
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
# Add redirect url on which user is redireced after authentication
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/login/'
- Create new module with any name (sauth in this repo) and do not foget to add it to INSTALLED_APPS
- Add path('', include('sauth.urls')) to urls.py file in new created file.
- Create view and template file in this module.
- Create new view in view.py
from django.views.generic import TemplateView
class LoginView(TemplateView):
template_name = "login.html"
- in templates/login.html add following HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<a href="{% url "social:begin" "google-oauth2" %}">Google+</a>
<p>{{ user.is_authenticated }} {{ user.username }}</p>
</body>
</html>
- In sauth/urls.py add new url poining to the view created above.
path('', include('social_django.urls', namespace='social')),
- Include these urls to root app urls.py ie
path('', include('sauth.urls'))