My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsingTheUsersService  
modeled after http://code.google.com/appengine/docs/python/gettingstarted/usingusers.html
Tutorial, appengine_0.0.6
Updated Dec 10, 2009 by brian.gi...@gmail.com

Using the Users Service

Google App Engine provides several useful services based on Google infrastructure, accessible by applications using libraries included with the SDK. One such service is the Users service, which lets your application integrate with Google user accounts. With the Users service, your users can use the Google accounts they already have to sign in to your application.

Let's use the Users service to personalize this application's greeting.

Using Users

Edit guestbook.rb and replace its contents with the following:

require 'sinatra'
require 'dm-core'
require 'appengine-apis/users'

get '/' do
  user = AppEngine::Users.current_user

  if user
    "Hello, #{user.nickname}"
  else
    redirect AppEngine::Users.create_login_url(request.url) 
  end
end

Restart the dev server and reload the page in your browser. Your application redirects you to the local version of the Google sign-in page suitable for testing your application. You can enter any username you'd like in this screen, and your application will see a fake User object based on that username.

When your application is running on App Engine, users will be directed to the Google Accounts sign-in page, then redirected back to your application after successfully signing in or creating an account.

The Users API

Let's take a closer look at the new pieces:

user = AppEngine::Users.current_user

If the user is already signed in to your application, AppEngine::Users.current_user returns the User object for the user. Otherwise, it returns nil.

if user
  "Hello, #{user.nickname}"

If the user has signed in, display a personalized message, using the nickname associated with the user's account.

else
  redirect_to AppEngine::Users.create_login_url(request.url)

If the user has not signed in, tell the application to redirect the user's browser to the Google account sign-in screen. The redirect includes the URL to this page (request.request_uri) so the Google account sign-in mechanism will send the user back here after the user has signed in or registered for a new account.

For more information about the Users API, see the API Reference.

Next...

Our application can now greet visiting users by name. Let's add a feature that will let users greet each other.

Continue to HandlingForms.


Sign in to add a comment
Powered by Google Project Hosting