I can't retrieve GET values

52 views Asked by At

I have a system in CakePHP that performs login with Google account and then returns to the home page. The page www.mysite.com/login/google identifies that there is no code variable and redirects to the Google login page, which is fine. However, when it confirms the login and redirects to www.mysite.com/login/google?code=1234, the system does not recognize the code variable and redirects again to the Google login page.

controller:

namespace App\Controller;

use App\Controller\AppController;
use Cake\Http\Client;
use Cake\Http\ServerRequest;
use App\Model\Table\TusuarioTable;
use Cake\ORM\TableRegistry;

class GoogleLoginController extends AppController
{
    public function login($code_ = null)
    {

        $this->request->allowMethod(['get','post', 'put']);

        // Configurações do Google OAuth
        $google_oauth_client_id = '43534534543354pps.googleusercontent.com';
        $google_oauth_client_secret = 'pP6rtMcj5U_zuK';
        $google_oauth_redirect_uri = 'https://sistemasweb.com/login/google';
        $google_oauth_version = 'v3';

        $code_id = $this->request->getQuery('code');

        //var_dump($_GET); exit;
       
        // Se o código de autorização estiver presente na URL
        if (isset($code_id)) {
            // Executar solicitação para obter o token de acesso

            echo "CODE: ".$code_id; exit;
        } else {
            // Redirecionar para a página de autorização do Google
            $params = [
                'response_type' => 'code',
                'client_id' => $google_oauth_client_id,
                'redirect_uri' => $google_oauth_redirect_uri,
                'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
                'access_type' => 'offline',
                'prompt' => 'consent'
            ];

            return $this->redirect('https://accounts.google.com/o/oauth2/auth?' . http_build_query($params));
        }
    }

    public function logout()
    {
        $this->request->getSession()->delete('google_loggedin');
        $this->request->getSession()->delete('google_email');
        $this->request->getSession()->delete('google_name');
        $this->request->getSession()->delete('google_picture');

        // Redirecionar para a página de login ou outra página relevante
        return $this->redirect(['controller' => 'Pages', 'action' => 'home']);
    }
}

0

There are 0 answers