I'm using django social_auth for user identification and it works great when user first login. However, if access token expired or directly open page by url, user will be redirect to a default page ('/') after google oauth. What should I do to get original page after google oauth login?
Here is my settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'bootstrap3',
'djangobower',
'social_auth',
)
AUTHENTICATION_BACKENDS = (
'social_auth.backends.google.GoogleOAuth2Backend',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
GOOGLE_OAUTH2_CLIENT_ID = 'CLIENT_ID'
GOOGLE_OAUTH2_CLIENT_SECRET = 'CLIENT_SECRET'
SOCIAL_AUTH_USER_MODEL = 'auth.User'
SESSION_SERIALIZER='django.contrib.sessions.serializers.PickleSerializer'
GOOGLE_WHITE_LISTED_DOMAINS = ['google.com']
SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {
'access_type': 'offline',
'approval_prompt': 'select_account+consent',
'prompt' : 'select_account+consent'
}
login.html:
{% load bootstrap3 %}
{% load static %}
<link media="all" href="{% static 'bootstrap/dist/css/bootstrap.min.css' %}" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src='{% static 'bootstrap/dist/js/bootstrap.min.js' %}'></script>
{% load static %}
<script type="text/javascript" src='{% static 'jquery/jquery.js' %}'></script>
<link media="all" href="{% static 'dashboard.css' %}" type="text/css" rel="stylesheet"/>
<script src='{% static 'canvasjs/canvasjs.min.js' %}' type="text/javascript"></script>
<link rel="stylesheet" href="{% static 'signin.css' %}" />
{% if form.errors %}
<p text-align="center">Your username and password didn't match. Please try again. {{form.errors}} </p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-signin" role="form">
{% csrf_token %}
<h2 class="form-signin-heading">ABC Website</h2>
<input id="id_username" maxlength="254" name="username" type="text" class="form-control" placeholder="Username" autofocus/>
<input id="id_password" name="password" type="password" class="form-control" placeholder="Password"/>
<button class="btn btn-lg btn-primary btn-block" type="submit">System Account LogIn</button>
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p style="text-align: center"><a href="{% url "socialauth_begin" "google-oauth2" %}?prompt=select_account+consent">GMail Login</a></p>
urls.py:
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
url(r'^accounts/logout/$', login_required(views.logout_view), name = "logout"),
url(r'^complete/(?P<backend>[^/]+)/$', views.AuthComplete.as_view()),
url(r'^login-error/$', views.LoginError.as_view(), 'login-error'),
url(r'', include('social_auth.urls')),
url(r'^admin/', include(admin.site.urls)),
In your html template you could have this within your login button to redirect to the same page:
href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path }}"
"next=" specifies where to link to next after logging in. So "next={{ request.path }}" links to the same path as the original request.
If you're just redirecting to the homepage then just use "next=/":
href="{% url 'social:begin' 'google-oauth2' %}?next=/"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With