Skip to content

Instantly share code, notes, and snippets.

@adamalton
Last active September 21, 2016 09:45
Show Gist options
  • Save adamalton/8789098 to your computer and use it in GitHub Desktop.
Save adamalton/8789098 to your computer and use it in GitHub Desktop.
Google multiple sign-in on App Engine
from django.shortcuts import redirect
from google.appengine.api.users import (
create_login_url,
create_logout_url,
get_current_user,
)
def switch_accounts(request):
""" View function for allowing a user to switch Google accounts.
Requires 'destination_url' in the query string for the URL
to redirect the user to after they have switched accounts.
"""
current_user_id = get_current_user().user_id()
previous_user_id = request.session.get('original_user_id')
if previous_user_id and previous_user_id != current_user_id:
#They have successfully switched accounts
del request.session['original_user_id']
final_destination = request.GET['destination_url']
return redirect(final_destination)
if previous_user_id and previous_user_id == current_user_id:
#They’ve been to the login view and returned as the same
#user, so they’re not using the multiple accounts feature.
#To switch accounts they must log out, then back in again.
login_url = create_login_url(dest_url=request.get_full_path())
return redirect(create_logout_url(dest_url=login_url)))
#else, we haven’t tried the multiple accounts test yet..
request.session['original_user_id'] = current_user_id
login_url = create_login_url(dest_url=request.get_full_path())
return redirect(login_url)
@ana-balica
Copy link

Line 25 has an extra closing parenthesis. Should be return redirect(create_logout_url(dest_url=login_url)).
Basically a fix for those who are lazy and like to copy-paste : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment