I have a problem from a few days on my site on -Sign in with google-. It automatically signs in when access the page. This wasn’t happened before, and i didn’t change anything on code. I don’t see where could be the problem…
Here is the code from my pages.
Google sign in button:
<div id="my-signin2" class="googleSignIn" style="align:center"></div>
Script:
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
<script>
function onLoad() {
gapi.load('auth2', function() {
gapi.auth2.init();
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': loginSuccess,
});
});
}
function loginSuccess(userInfo) {
var data = userInfo.getBasicProfile();
//ajax call update data in database
$.ajax({
type: "POST",
url: "update_data.php",
data: {
'id': data.getId(),
'name': data.getName(),
'image': data.getImageUrl(),
'email': data.getEmail(),
},
success: function(result) {
window.location.href = "page to redirect";
}
});
}
</script>
update_data.php:
session_start();
include_once 'config.php';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$user_id = $_POST['id'];
$name = $_POST['name'];
$image = $_POST['image'];
$email = $_POST['email'];
$sql = "SELECT * FROM utilizatori WHERE email_utilizator = :email";
$query = $dbh->prepare($sql);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_ASSOC);
if($query->rowCount() > 0)
{
foreach($results as $result)
{
$_SESSION['USER_NAME']= $name;
$_SESSION['USER_EMAIL']= $result['email_utilizator'];
$_SESSION['USER_ID']= $result['id_utilizator'];
$_SESSION['USER_IMAGE']= $image;
$_SESSION['USER_TYPE']= $result['tip_utilizator'];
}}
else
echo 'Nu exista utilizatorul';
logout.php
<?php
session_start();
include('config.php');
session_destroy();
echo "<script type='text/javascript'> document.location ='index.php'; </script>";
?>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>