32 |
32 |
33 |
33 |
34 def login_required(handler_method): |
34 def login_required(handler_method): |
35 """A decorator to require that a user be logged in to access a handler. |
35 """A decorator to require that a user be logged in to access a handler. |
36 |
36 |
37 To use it, decorate your get() or post() method like this: |
37 To use it, decorate your get() method like this: |
38 |
38 |
39 @login_required |
39 @login_required |
40 def get(self): |
40 def get(self): |
41 user = users.GetCurrentUser(self) |
41 user = users.GetCurrentUser(self) |
42 self.response.out.write('Hello, ' + user.nickname()) |
42 self.response.out.write('Hello, ' + user.nickname()) |
43 |
43 |
44 We will redirect to a login page if the user is not logged in. We always |
44 We will redirect to a login page if the user is not logged in. We always |
45 redirect to the request URI, and Google Accounts only redirects back as a GET request, |
45 redirect to the request URI, and Google Accounts only redirects back as a GET |
46 so this should not be used for POSTs. |
46 request, so this should not be used for POSTs. |
47 """ |
47 """ |
48 def check_login(self, *args): |
48 def check_login(self, *args): |
49 if self.request.method != 'GET': |
49 if self.request.method != 'GET': |
50 raise webapp.Error('The check_login decorator can only be used for GET ' |
50 raise webapp.Error('The check_login decorator can only be used for GET ' |
51 'requests') |
51 'requests') |