Thursday, February 7, 2013

Logging out of AppEngine App only

I wanted to create a logout URL in an Google AppEngine app, but unfortunately the documented method (users.create_logout_url()) logs you out of all Google services, not just the app.

Fortunately, I found this post which describes how to set cookies to log you out of the app.  Here is what I did in my app:


class LogoutPage(webapp2.RequestHandler):
def get(self):
target_url = self.request.referer or '/'
if os.environ.get('SERVER_SOFTWARE', '').startswith('Development/'):
self.redirect(users.create_logout_url(target_url))
return
# On the production instance, we just remove the session cookie, because
# redirecting users.create_logout_url(...) would log out of all Google
# (e.g. Gmail, Google Calendar).
#
# It seems that AppEngine is setting the ACSID cookie for http:// ,
# and the SACSID cookie for https:// . We just unset both below.
cookie = Cookie.SimpleCookie()
cookie['ACSID'] = ''
cookie['ACSID']['expires'] = -86400 # In the past, a day ago.
self.response.headers.add_header(*cookie.output().split(': ', 1))
cookie = Cookie.SimpleCookie()
cookie['SACSID'] = ''
cookie['SACSID']['expires'] = -86400
self.response.headers.add_header(*cookie.output().split(': ', 1))
self.redirect(target_url)
app = webapp2.WSGIApplication( [
(r'/logout', LogoutPage ),
],
debug=True)
view raw gistfile1.py hosted with ❤ by GitHub
https://gist.github.com/anonymous/4733505 

Now, I can add a link to /logout, and the user will be logged out of the app.

Many thanks to the pts.blog!

No comments:

Post a Comment