Recently I implemented google login in my opencart project and everything is fine when user is created, login/logout. But for account created with google, when I enter shipping adress and press continue is logging me out and this error appear: “Invalid token” and logs me out.
This is the code I used in login.php
require_once(DIR_OPENCART . 'vendor/autoload.php');
$clientID = 'xxxxxxxxxx';
$data['google_clientID'] = $clientID;
$client = new Google_Client();
$client->setClientId($clientID);
$client->setClientSecret('xxxxxxxxxxxxxx');
$client->setRedirectUri('xxxxxxxxxxx.net/index.php?route=account/account');
$client->addScope(["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "openid"]);
$googleToken = $_GET['google_token'] ?? null;
$isGoogleLogged = false;
if ($googleToken)
{
$payload = $client->verifyIdToken($googleToken);
if ($payload)
{
$userid = $payload['sub'];
$this->load->model('account/customer');
$customer_info = $this->model_account_customer->getCustomerByEmail($payload['email']);
if (!$customer_info)
{
$customer_data = array(
'customer_group_id' => 1,
'store_id' => 0,
'language_id' => $this->session->data['language_id'] ?? $this->config->get('config_language_id'),
'firstname' => $payload['given_name'] ?? '',
'lastname' => $payload['family_name'] ?? '',
'email' => $payload['email'],
'telephone' => '',
'password' => uniqid(),
'custom_field' => '',
'newsletter' => 0,
'ip' => $this->request->server['REMOTE_ADDR'],
'status' => 1,
'safe' => 0,
'token' => $userid,
'date_added' => time(),
);
$this->model_account_customer->addCustomer($customer_data);
}
$this->customer->login($payload['email'], '', true);
$this->session->data['customer_token'] = oc_token(26);
$isGoogleLogged = true;
}
}
// ----------------------- GOOGLE LOGIN ---------------------------------------------------------------------
// Original opencart code
// // If already logged in and has matching token then redirect to account page
// if ($this->customer->isLogged() && isset($this->request->get['customer_token']) && isset($this->session->data['customer_token']) && ($this->request->get['customer_token'] == $this->session->data['customer_token'])) {
// $this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']));
// }
// Added google login check when redirecting to account page
if (($this->customer->isLogged() && isset($this->request->get['customer_token']) && isset($this->session->data['customer_token'])
&& ($this->request->get['customer_token'] == $this->session->data['customer_token'])) || $isGoogleLogged) {
$this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']));
}
and this in my login.twig file
<script src="https://accounts.google.com/gsi/client" async></script>
<div id="g_id_onload"
data-client_id={{ google_clientID }}
data-context="signin"
data-ux_mode="popout"
data-callback="handleCredentialResponse"
data-itp_support="true"
data-use_fedcm_for_prompt="true">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="outline"
data-text="signin_with"
data-size="large"
data-logo_alignment="left">
</div>
<script>
function handleCredentialResponse(response) {
// console.log("Received Credential:", response.credential);
window.location.href = `xxxxxxxxxxxxxxxxxxxxxx/index.php?route=account/login&google_token=${encodeURIComponent(response.credential)}`;
}
</script>
New contributor
ashy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.