package com.google.step.coffee.servlets;

import com.google.api.client.auth.oauth2.Credential;
import com.google.step.coffee.JsonServlet;
import com.google.step.coffee.JsonServletRequest;
import com.google.step.coffee.OAuthService;
import com.google.step.coffee.UserManager;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;

/**
 * Manage and check user authorisation for scopes for Google Calendar API in
 * <code>OAuthService</code>
 */
@WebServlet("/api/auth/calendar")
public class AuthCalendarServlet extends JsonServlet {

  /** Response object for fetching authorisation status of user. */
  private static class CalAuthResponse {
    private boolean authorised;
    private String authLink;

    CalAuthResponse(boolean authorised) {
      this.authorised = authorised;
    }

    CalAuthResponse(boolean authorised, String authLink) {
      this(authorised);
      this.authLink = authLink;
    }
  }

  @Override
  public Object get(JsonServletRequest request) throws IOException {
    CalAuthResponse responseData;
    Credential credentials = OAuthService.getCredentials(UserManager.getCurrentUserId());

    if (credentials != null) {
      responseData = new CalAuthResponse(true);
    } else {
      responseData = new CalAuthResponse(false, OAuthService.getAuthURL(request));
    }

    return responseData;
  }
}

@@ -1046,188 +1046,80 @@
 ta;%0A
+%0A
     
-Credential credentials = OAuthService.getCredentials(UserManager.getCurrentUserId());%0A%0A    if (credentials != null) %7B%0A      responseData = new CalAuthResponse(true);%0A    %7D else
+if (OAuthService.userHasAuthorised(UserManager.getCurrentUserId()))
  %7B%0A 
@@ -1201,24 +1201,85 @@
 est));%0A    %7D
+ else %7B%0A      responseData = new CalAuthResponse(true);%0A    %7D
 %0A%0A    return

Change credential check to not fetch credential to check existence