title: Server-side redirect if not logged in
body: <p>I use Cloud Endpoints and do client-side oauth to access my backend. This works fine, except that it means I can only check whether the person hitting up my app URL is a user or not after the whole page and attached scripts have loaded and the oauth request has completed.</p>
  
  <p>I'd like to enable server-side authentication so that I can use the same URL for my landing page and my app, and redirect the person to the app if she is a user or to the landing page if she isn't.</p>
  
  <p>The problem is that the <a href="https://cloud.google.com/appengine/docs/java/gettingstarted/ui_and_code" rel="nofollow">User Tutorial</a> uses the <a href="https://cloud.google.com/appengine/docs/java/users/" rel="nofollow">UserService</a> for authentication whereas Cloud Endpoints uses the <a href="https://cloud.google.com/appengine/docs/java/oauth/#Java_OAuth_and_App_Engine" rel="nofollow">OAuth Service</a>. I can't figure out how to use OAuth in my <a href="https://cloud.google.com/appengine/docs/java/config/webxml#Filters" rel="nofollow">redirect filter</a> because all the links from the OAuth section linked to above are for OAuth 1 and <a href="https://developers.google.com/accounts/docs/OAuth_ref#RequestToken" rel="nofollow">are deprecated</a>.</p>
  
  <p>How can I check in my redirect filter whether the person visiting is a user or not and simultaneously get a token that I can use to subsequently call my endpoints if she is a user?</p>
  
  <p>I'm using the <a href="https://developers.google.com/api-client-library/javascript/reference/referencedocs" rel="nofollow">Google Api JS client</a> to make calls to cloud endpoints, so the solution would optimally integrate with that, using <code>gapi.auth.setToken(token)</code> with the token retrieved and bypass the whole <code>gapi.auth.authorize(params, callback)</code> dance.</p>