Using the Users Service

Google App Engine provides several useful services based on Google infrastructure. One example is the Users service, which lets your application integrate with Google user accounts. With the Users service, your users can employ the Google accounts they already have to sign in to your application.

The Users service makes it easy to personalize this application's greeting.

Using Users

Edit myapp/hello.go again, and replace its contents with the following:

Reload the page in your browser. Your application presents you with a link that, when followed, will redirect you to the local version of the Google sign-in page suitable for testing your application. You can enter any username you want, but the value that is actually used when running on the local development server is test@example.com.

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:

The appengine.NewContext function returns an appengine.Context value associated with the current request. This is an opaque value used by many functions in the Go App Engine SDK to communicate with the App Engine service.

If the user is already signed in to your application, user.Current returns a pointer to a user.User value for the user. Otherwise, it returns nil.

If the user has not signed in, redirect the user's browser to the Google account sign-in screen by setting a Location header and returning an HTTP status code of 302 "Found". The redirection includes the URL to this page (r.URL.String()) so the Google account sign-in mechanism will send the user back here after signing in or registering for a new account.

The user.LoginURL function returns an error value as its second argument. Though an error is unlikely to occur here, it is good practice to check it and display an error user, if appropriate (in this case, with the http.Error helper).

If the user has signed in, display a personalized message using the name associated with the user's account. In this case, the fmt.Fprintf function calls *user.User's String method to get the user's name in string form.

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

Note: for specific paths that should be served only to logged in users, use the login: required directive in the app.yaml file. See the app.yaml reference for details.


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

Handling Forms >>