PHP Authentication with Google One Tap

Viewed 664

I've insert "Google One Tap" in my website, then Google returns me "credential" and "g_csrf_token" with POST method. Now I want to know, how I get email address from this "credential" in PHP?

Is there any PHP library or any module for Codeigniter 4?

I have only this in Frontend:

<script src="https://accounts.google.com/gsi/client" async defer></script>
<div id="g_id_onload" data-client_id="000.apps.googleusercontent.com" data-login_uri="https://myweb.com/google"></div>

And in backend file "myurl/google"

print_r($_POST);

Thanks

2 Answers

Here is how to get it:

<?php
 $token=$_POST["credential"];
 print_r(json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $token)[1])))));
?>

Here is how we get it, according to this docs:

Here

verify-google-id-token

image 1

And here :

Server-side integration

image 2

// Firstly, create Google client object with client ID

$google_client = new Google_Client([
    'client_id' => YOUR_CLIENT_ID
]);

// Then, verify the credential

$token = $_POST['credential'];

$payload = $google_client->verifyIdToken($token);
if ($payload && $payload['aud'] == YOUR_CLIENT_ID)
{

    // Here you can get user email from Google, Also you can get all user data

    $email = $payload['email'];

    // Lastly, you can add user email to session, Or any thing you want in your app

    $_SESSION['user'] = $email;

}
Related