arttuladhar
12/29/2012 - 7:42 PM

SignOn features using Google Web Services is easy and efficient way to authenticate users. It's pretty common these days to have authenticat

SignOn features using Google Web Services is easy and efficient way to authenticate users. It's pretty common these days to have authentication via Google UserServices. Google App Engine provides UserServiceFactory api for this purpose. I tried implementing this API within Google Web Toolkit and here's the code snippet for reference.

The code is divided to Server Side Code and Client Side Code. Server side code provides the implementation of the actual API call while the Client side code provides interface to those RPC calls and you can make asynchronous call to the server side api. Deeper understanding of RPC call can be found in my previous weblogs.

package com.aayush.hangman.server;

import com.aayush.hangman.client.LoginServices;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class LoginServicesImpl extends RemoteServiceServlet implements LoginServices{
  
	final static public UserService userService = UserServiceFactory.getUserService();

	@Override
	public String createLoginURL(String callbackURL) {
		return userService.createLoginURL(callbackURL);
	}

	@Override
	public String createLogoutURL(String callbackURL) {
		return userService.createLogoutURL(callbackURL);
	}

	@Override
	public String getEmail() {
		return userService.getCurrentUser().getEmail();
	}

	@Override
	public boolean isLoggenIn() {
		return userService.isUserLoggedIn();
	}

}
package com.aayush.hangman.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface LoginServicesAsync {

  void createLoginURL(String callbackURL, AsyncCallback<String> callback);

	void createLogoutURL(String callbackURL, AsyncCallback<String> callback);

	void getEmail(AsyncCallback<String> callback);

	void isLoggenIn(AsyncCallback<Boolean> callback);

}
package com.aayush.hangman.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("loginservices")
public interface LoginServices extends RemoteService{
  
	String createLoginURL( String callbackURL );
	String createLogoutURL ( String callbackURL );
	
	String getEmail();
	boolean isLoggenIn();

}
package com.aayush.hangman.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class HangMan implements EntryPoint {

final LoginServicesAsync logininfo = GWT.create(LoginServices.class);

public void onModuleLoad() {

  	// Create Interface
		RootPanel hangman_login = RootPanel.get("signon");
		createloginScreen(hangman_login);
	}

	private void createloginScreen(final RootPanel hangman_login) {

		logininfo.isLoggenIn(new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error : Sorry Cannot Connect to Google Services");
				hangman_login.add(new Label("Cannot Connect to Google Services"));

			}

			@Override
			public void onSuccess(Boolean result) {

				if (result) {
					getEmail();
				} else {
					getsigninurl();
				}

			}

			private void getsigninurl() {
				logininfo.createLoginURL(GWT.getHostPageBaseURL(),new AsyncCallback<String>() {

							@Override
							public void onSuccess(String result) {
								Window.alert(result);
								String login_url = "<a href=\" " + result + "\">SignIn</a>";
								hangman_login.add(new HTML(login_url));

							}

							@Override
							public void onFailure(Throwable caught) {
								hangman_login.add(new Label("Cannot Connect to Google Services"));

							}
						});

			}

			private void getEmail() {

				logininfo.getEmail(new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						String userwelcome = "<h4>Welcome, " + result + "</h4>";
						hangman_login.add(new HTML(userwelcome));

						// Get SignOut URL
						logininfo.createLogoutURL(GWT.getHostPageBaseURL(),
								new AsyncCallback<String>() {

									@Override
									public void onSuccess(String result) {
										String logout = "<a href=\"" + result+ "\">LogOff</a><br><br>";
										hangman_login.add(new HTML(logout));

									}

									@Override
									public void onFailure(Throwable caught) {
										hangman_login.add(new Label("Cannot Connect to Google Services"));

									}
								});

					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Cannot Perform RPC Call");
						hangman_login.add(new Label("Cannot Connect to Google Services"));

					}
				});

			}
		});

	}