Authenticating users
The first requirement for our Notes application is showing the home page only to users who are logged in and redirect others to the login form; the users service provided by App Engine is exactly what we need and adding it to our MainHandler
class is quite simple:
import webapp2 from google.appengine.api import users class MainHandler(webapp2.RequestHandler): def get(self): user = users.get_current_user() if user is not None: self.response.write('Hello Notes!') else: login_url = users.create_login_url(self.request.uri) self.redirect(login_url) app = webapp2.WSGIApplication([ ('/', MainHandler) ], debug=True)
The user
package we import on the second line of the previous code provides access to users' service functionalities. Inside the get()
method of the MainHandler
class, we first check whether the user visiting the page has logged in or not. If they have, the get_current_user()
method...