Welcome Guest, Not a member yet? Register   Sign In
Integrating GOOGLE LOGIN in my CI4 App failing when move to Cpanel live Server
#1
Sad 

Hello there,
Am new here, but i need your help please. 
Through  couple of tutorials online, was able to get the GOOGLE LOGIN work perfectly on my Web App on my Xampp localhost server. But when I moved same code onto live Web App running on Linux Cpanel Server, got an error "403.shtml is not a valid controller name".
Here is the code.
Controller - Login.php
Code:
<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\LoginModel;

class Login extends Controller
{

public $loginModel;
public $session;
public $email;
public $googleClient=NULL;
public function __construct(){
helper('form');
$this->loginModel = new LoginModel();
$this->session = session();
$this->email = \Config\Services::email();
include_once APPPATH.'Libraries/vendor/autoload.php';
$this->googleClient = new \Google_Client();

$this->googleClient->setClientId('clientID');
$this->googleClient->setClientSecret('SecretID');
$this->googleClient->setRedirectUri('https://mydomain.com/login/gauth');
$this->googleClient->setAccessType('online');
$this->googleClient->addScope('email');
$this->googleClient->addScope('profile');

}

public function index()
{
$data = [];
//Here code for site login

//Google login button
$loginButton = '';
          if(!session()->set("AccessToken"))
          {
          $loginButton = $this->googleClient->createAuthUrl();
          $data['loginButton'] = $loginButton;
          return view('login', $data);
          }
          else
          {
          return view('login', $data);
          }

return view('login', $data);
echo view('includes/footer');
}

public function gauth()
{
    $session = session();

$token = $this->googleClient->fetchAccessTokenWithAuthCode($_GET['code']);

   
if(!isset($token['error'])){
$this->googleClient->setAccessToken($token['access_token']);
session()->set("AccessToken", $token['access_token']);

$googleService = new \Google_Service_Oauth2($this->googleClient);
$data = $googleService->userinfo->get();
$currentDateTime = date("Y-m-d H:i:s");


$userdata=array();
if($this->loginModel->google_user_exists($data['id']))
{

//User ALready Login and want to Login Again
$userdata = [
'verification_code'      => $data['id'],
'first_name'      => $data['given_name'],
            'last_name'        => $data['family_name'],
'email'            => $data['email'],
            'profile_pic'      => $data['picture'],
'last_modified'    => $currentDateTime
];
$this->loginModel->updateGoogleUser($userdata, $data['id']);
session()->set("logged_user", $userdata['verification_code']);

}
else
{
//new User want to Login
$userdata = [
'social_provider' => Google,
'status' => active,
'verification_code'      => $data['id'],
            'first_name'    => $data['given_name'],
            'last_name'    => $data['family_name'],
            'email'        => $data['email'],
            'profile_pic'  => $data['picture'],
'created_at'    => $currentDateTime
];
$this->loginModel->createGoogleUser($userdata);
}

session()->set("logged_user", $userdata['verification_code']);
}
else
{
    session()->setFlashData("Error", "Something went Wrong");
return redirect()->to(base_url());
}

//Successfull Login
return redirect()->to(base_url()."/home");
}
}


View - login.php

<div class="form-group">
<a href="<?= $loginButton ?>" class="form-control btn btn-danger rvn_obtn mt-3"><i class="bi bi-google px-3"></i> Login with Google</a>
</div>

Looking forward hearing from you.
Reply
#2

you shouldn't need this line
Code:
include_once APPPATH.'Libraries/vendor/autoload.php';
CMS CI4     I use Arch Linux by the way 

Reply
#3

(01-11-2022, 01:44 AM)captain-sensible Wrote: you shouldn't need this line
Code:
include_once APPPATH.'Libraries/vendor/autoload.php';
Thank you @captain-sensible for the reply.

Unfortunately when i comment it out, I get error "Class 'Google_Client' not found"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB