mike29892
mike29892

Reputation: 263

App Engine Python Authentication Custom Redirect

handlers:
- url: /secure_api/.*
  script: _go_app
  login: required
  auth_fail_action: unauthorized

This code only brings me to a page saying "Login required to view page." Is there a way to instead redirect to my home page?

Upvotes: 1

Views: 305

Answers (1)

RocketDonkey
RocketDonkey

Reputation: 37269

When you specify auth_fail_action: unauthorized, you get the page you are seeing (see here for the details). Changing unauthorized to redirect will take them to the login screen, but if you want to do more granular handling of users based on their logged-in status, your best bet is to do it inside of your code via the Users API. For instance (this is adapted from the docs), here is a simple example that would redirect a non-logged-in user to /:

from google.appengine.api import users
import webapp2

class MyHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            # Do stuff here for logged-in users
        else:
            # Redirect if user is None, which is the case for non-logged-in users
            self.redirect('/')

Upvotes: 2

Related Questions