1

I have this code:

def handler_method():
    users = UserModel.query(UsersModel.email == users.get_current_user().email()).fetch()
    if len(users) == 0:
        user = UsersModel(email = users.get_current_user().email())
        user.put()

class MyHandler(webapp2.RequestHandler):
    @login_required
    def get(self):
        .
        .
        my code
        .
        .

How can I specify to the @login_required decorator the method "handler_method" as the action to perform once the Login success ?

Fethi Dilmi
  • 161
  • 3
  • 16
  • possible duplicate of [Required login using decorator using gae python. passing parameters?](http://stackoverflow.com/questions/14535196/required-login-using-decorator-using-gae-python-passing-parameters) – Jimmy Kane Dec 30 '13 at 13:09

2 Answers2

1

Looking at the code for the decorator:

def login_required(handler_method):
  """..."""
  def check_login(self, *args):
    if self.request.method != 'GET':
      raise webapp.Error('The check_login decorator can only be used for GET '
                         'requests')
    user = users.get_current_user()
    if not user:
      self.redirect(users.create_login_url(self.request.uri))
      return
    else:
      handler_method(self, *args)
  return check_login 

there isn't a way to run additional code if the user is logged in successfully.

You could write a custom version of the decorator that would do what you want:

def custom_login_required(handler_method):
  """..."""
  def check_login(self, *args):
    if self.request.method != 'GET':
      raise webapp.Error('The check_login decorator can only be used for GET '
                         'requests')
    user = users.get_current_user()
    if not user:
      self.redirect(users.create_login_url(self.request.uri))
      return
    else:
      users = UserModel.query(UsersModel.email == users.get_current_user().email()).fetch()
      if len(users) == 0:
          user = UsersModel(email = users.get_current_user().email())
          user.put()
      handler_method(self, *args)
  return check_login 
Ryan Morlok
  • 796
  • 8
  • 13
  • @Ryan Morlok we will use it like decorator above any function we want; but what if we want to pass any arguments like the list of the roles for example @custom_login_required(['admin','role1','role2']). and consuming this list of roles in check_login function In this case what we will do? – Saim Abdullah Nov 22 '18 at 06:41
  • Things get more complicated when you want to take parameters to a decorator, especially if you want to make those parameters optional. There are other questions which cover how to do this (e.g. https://stackoverflow.com/questions/5929107/decorators-with-parameters) – Ryan Morlok Dec 01 '18 at 15:56
0

You should use the users.get_current_user() to identify whether user is logged-in:

from google.appengine.api import users

class MyHandler(webapp2.RequestHandler):
    @login_required
    def get(self):
        if users.get_current_user():
            handler_method()
        else:
            #user not login, redirect or return 404
            pass
Colin Su
  • 4,613
  • 2
  • 19
  • 19
  • The login_required decorator will mean there is always a current-user when the code runs – Greg Dec 30 '13 at 15:34