<?php





/*


 * To change this license header, choose License Headers in Project Properties.


 * To change this template file, choose Tools | Templates


 * and open the template in the editor.


 */





/**


 * Description of GoogleAuth


 *


 * @author Desarrollo


 */





 class GoogleAuth {





    protected $client;





    public function __construct(Google_Client $googleClient) {


        $this->client = $googleClient;


        // if ($this->client) {


        //     $this->client->setClientId('424279050038-5911u0ktr552i7rj4o7uh3kfu0s4ge1g.apps.googleusercontent.com');


        //     $this->client->setClientSecret('f9ouxiLeiZvq6Z-hUASg7762');


        //     $this->client->setRedirectUri('https://sicam.ccsm.org.co/');


        //     $this->client->setScopes('email');


        // }


    }





    public function getAuthUrl() {


        return $this->client->createAuthUrl();


    }





    public function checkRedirectCode() {


        if (isset($_GET['code'])) {


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


            $this->setToken($token);


            $email = $this->getPayload($token);


            $Usuario = Usuarios::usuarioPorUsername($email);


            if (!empty($Usuario)) {


                $_SESSION['logueado'] = true;


                $Colaborador = Colaboradores::datosCompletosPorUsuario($Usuario->usuarioID);


                $_SESSION['Usuario'] = $token;


                $_SESSION['logueadoGoogle'] = true;


                unset($_GET['code']);


                return true;


            } else {


                return false;


            }


        }


        return false;


    }





    public function setToken($token) {


        $_SESSION['access_token'] = $token;


        $this->client->setAccessToken($token);


    }





    public function getPayload() {


        $dataEmail = $this->client->verifyIdToken();


        return $dataEmail['email'];


    }





 }




