Using the User service
Wednesday, February 03, 2010
4:27 PM
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
// …
Label userLabel = new Label(""); // <td id="userLabelContainerID></td>
Anchor loginAnchor = new Anchor();
// ...
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
     userLabel.setText("Hello, "+user.getNickname());
     loginAnchor.setHref(userService.createLogoutURL("/")); 
     loginAnchor.setText("logout"); 
} else { 
     userLabel.setText("Hello guest!"); 
     loginAnchor.setText("login");
     loginAnchor.setHref(userService.createLoginURL("/"));
} 
// createLoginURL creates a URL with an implicit reference
// to *your* page, so that when you login, you get bounced back
// via a redirect