Im trying to implement the new google Google Identity Service google login in my website, however I ran into some issues. Basically I have two types of creating account on my page, one is by manually filling out details and the other one is through google, however I can’t seem to make it work.
this is the exact error : Uncaught Error: Class ‘Google_Client’ not found in myfolder/accountCreation/create-account-google.php:19
I basically want to get the google account data and then insert it into my database where I store account details…
This is the html
<div id="g_id_onload"
data-client_id="My-Client-ID(I have it here).apps.googleusercontent.com"
data-callback="handleCredentialResponse"
data-context="signin"
data-ux_mode="popup"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="outline"
data-text="signin_with"
data-size="large"
data-logo_aligment="left">
</div>
This is the function
function handleCredentialResponse(response){
const responsePayload = decodeJwtResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log("Full Name: " + responsePayload.name);
console.log("Given name: " + responsePayload.given_name);
console.log("Family name: " + responsePayload.family_name);
console.log("Email: " + responsePayload.email);
$.ajax({
type: "POST",
url: "myfolder/accountCreation/create-account-google.php",
data: {
id_token: response.credential,
id: responsePayload.sub,
full_name: responsePayload.name,
given_name: responsePayload.given_name,
family_name: responsePayload.family_name,
email: responsePayload.email
},
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
//decode JWT
function decodeJwtResponse(data){
var tokens = data.split(".");
return JSON.parse(atob(tokens[1]));
}
}
This is my php script
<?php
include(dirname(__FILE__) . '/../connection.php');
if(isset($_POST['id_token'])) {
$id_token = $_POST['id_token'];
// Get user information from the ID token
$client = new Google_Client(['client_id' => 'My-Client-ID(I have it there).apps.googleusercontent.com']);
$payload = $client->verifyIdToken($id_token);
if ($payload) {
$email = $payload['email'];
$firstName = $payload['given_name'];
$lastName = $payload['family_name'];
// Check if the email already exists in the database
$sql = "SELECT * FROM loginregister WHERE email='$email'";
$result = $con->query($sql);
if($result->num_rows > 0) {
echo "Email already exists. Please choose a different email address.";
} else {
// Insert the new user information into the database
$sql = "INSERT INTO loginregister (email, firstname, lastname) VALUES ('$email', '$firstName', '$lastName')";
if ($con->query($sql) === TRUE) {
echo "Account created successfully. You can now login.";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
}
}
else {
echo "Invalid ID token.";
}
?>
I tried watching tutorials on youtube but unfortunately they are in react or are just not sufficient enough to fix my problem. Cant ask chatGPT because its outdated on this topic as its 2023, and chatGPT uses 2021 info.