lorenzo-s
lorenzo-s

Reputation: 17010

Google OAuth 2.0 redirect URL when user refuses/cancel consent screen

I'm migrating the Sign-in with Google on my PHP app from the old and now deprecated OpenID to the new OAuth 2.0.

All I need from user is his email address, so I can register or log him in. It's all working as expected, but I noticed that if user refuses the consent screen (with the Cancel button, the white one) the consent screen is displayed again. Is there a way I can specify a redirect URL for when user refuses the consent screen, so I can display a custom message to him?

My code is:

$client = new Google_Client();
$client->setAuthConfig(Config::$GOOGLE_API_AUTH_CONFIG_JSON);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->setRedirectUri(Config::$WEBSITE_URL . 'index.php?page=login&action=login');
if (!isset($_GET['code'])) {
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
    $client->authenticate($_GET['code']);
    $oauth2_service = new Google_Service_Oauth2($client);
    UserLogin::get()->signupOrLogin($oauth2_service->userinfo->get()->getEmail());
}

Upvotes: 4

Views: 2992

Answers (1)

Ian Barber
Ian Barber

Reputation: 19960

You can't specify another URL, but you can check in your code for the "error" parameter on the query string - this will be set if the the user cancels. See more about handling the response on https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

Upvotes: 4

Related Questions