So i'm working on putting google sign in/out into my website. I'm able to log a user in and out through the backend. The problem is that if you sign in then sign out then try to sign in again, google will not ask you which account you want to use. It just automatically uses your previous account.
How can I signout a user so when they sign back in, it prompts them to select an account?
Here's my code if anybody is curious
<?php session_start();
if(isset($_GET['logout']) && $_GET['logout'] == 1){
//logout
unset($_SESSION["access_token"]);
// setcookie('G_AUTHUSER_H', 'derp', strtotime( '1 second' ));
echo 'logout ran';
}
//https://github.com/google/google-api-php-client/tree/v1-master
include ($_SERVER[DOCUMENT_ROOT]."/google-api/autoload.php");
// ########## Google Settings.Client ID, Client Secret from https://console.developers.google.com #############
$client_id = 'xxxx';
$client_secret = 'xxxx';
$redirect_uri = 'http://localhost:8888/test2.php';
// ###################################################################
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$service = new Google_Service_Oauth2($client);
//If code is empty, redirect user to google authentication page for code.
//Code is required to aquire Access Token from google
//Once we have access token, assign token to session variable
//and we can redirect user back to page and login.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
exit;
}
//if we have access_token continue, or else get login URL for user
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
//Display user info or display login url as per the info we have.
echo '<div style="margin:20px">';
if (isset($authUrl)){
//show login url
echo '<div align="center">';
echo '<h3>Login with Google -- Demo</h3>';
echo '<div>Please click login button to connect to Google.</div>';
echo '<a class="login" href="' . $authUrl . '">Login</a>';
echo '</div>';
} else {
$user = $service->userinfo->get(); //get user info
$user_count = FALSE;
if($user_count)
{
echo 'Welcome back '.$user->name.'! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
}
else //else greeting text "Thanks for registering"
{
echo 'Hi '.$user->name.', Thanks for Registering! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
}
// echo $user->email;
echo '<pre>';
var_dump($user);
echo '</pre>';
}
echo '<br><br><br>Cookies';
var_dump($_COOKIE);
echo '<br><br><br>Sessions';
var_dump($_SESSION);
echo '</div>';
echo '<p>working</p>';
?>
If my question is unclear or confusing please say so.
You should add $client->setApprovalPrompt('select_account consent') to the new Google_Client().
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$client->setApprovalPrompt('select_account consent');