如何在Codeigniter中进行身份验证后重定向Google登录?

I'm a newbie in codeigniter and I'm trying to set up a test of a basic Google login using OAuth. It is basically one page with a sign-in button and if I click such button I go to the OAuth authentication screen. What I expect after authentication is that it redirects me to a profile page. The profile page reads data from a database after have been saved in the authentication.

I've been following this tutorial: https://www.codexworld.com/login-with-google-account-in-codeigniter/

Also I found this question: Redirect in codeigniter after login

It is exactly the same problem, but it isn't resolved and I don't understand the answer either.

Here's some code:

/controllers/User_authentication.php

<?php
class User_Authentication extends CI_Controller {

    function __construct(){
        parent::__construct();

        // Load google oauth library
        $this->load->library('google');

        // Load user model
        $this->load->model('user');
    }

    public function index(){
        // Redirect to profile page if the user already logged in
        if($this->session->userdata('loggedIn') == true){
            redirect('user_authentication/profile/');
        }

        if(isset($_GET['code'])){
            // Authenticate user with google
            if($this->google->getAuthenticate()){

                // Get user info from google
                $gpInfo = $this->google->getUserInfo();

                // Preparing data for database insertion
                $userData['oauth_provider'] = 'google';
                $userData['oauth_uid']      = $gpInfo['id'];
                $userData['first_name']     = $gpInfo['given_name'];
                $userData['last_name']      = $gpInfo['family_name'];
                $userData['email']          = $gpInfo['email'];
                $userData['gender']         = !empty($gpInfo['gender'])?$gpInfo['gender']:'';
                $userData['locale']         = !empty($gpInfo['locale'])?$gpInfo['locale']:'';
                $userData['link']           = !empty($gpInfo['link'])?$gpInfo['link']:'';
                $userData['picture']        = !empty($gpInfo['picture'])?$gpInfo['picture']:'';

                // Insert or update user data to the database
                $userID = $this->user->checkUser($userData);

                // Store the status and user profile info into session
                $this->session->set_userdata('loggedIn', true);
                $this->session->set_userdata('userData', $userData);

                // Redirect to profile page
                redirect('user_authentication/profile/');
            }
        } 

        // Google authentication url
        $data['loginURL'] = $this->google->loginURL();

        // Load google login view
        $this->load->view('user_authentication/index',$data);
    }

    public function profile(){
        // Redirect to login page if the user not logged in
        if(!$this->session->userdata('loggedIn')){
            redirect('/user_authentication/');
        }

        // Get user info from session
        $data['userData'] = $this->session->userdata('userData');

        // Load user profile view
        $this->load->view('user_authentication/profile',$data);
    }

    public function logout(){
        // Reset OAuth access token
        $this->google->revokeToken();

        // Remove token and user data from the session
        $this->session->unset_userdata('loggedIn');
        $this->session->unset_userdata('userData');

        // Destroy entire session data
        $this->session->sess_destroy();

        // Redirect to login page
        redirect('/user_authentication/');
    }

}

/models/User.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Model {

    function __construct() {
        $this->tableName = 'users';
        $this->primaryKey = 'id';
    }

    public function checkUser($data = array()){
        $this->db->select($this->primaryKey);
        $this->db->from($this->tableName);

        $con = array(
            'oauth_provider' => $data['oauth_provider'],
            'oauth_uid' => $data['oauth_uid']
        );
        $this->db->where($con);

        $query = $this->db->get();

        $check = $query->num_rows();

        if($check > 0){
            // Get prev user data
            $result = $query->row_array();

            // Update user data
            $data['modified'] = date("Y-m-d H:i:s");
            $update = $this->db->update($this->tableName, $data, array('id'=>$result['id']));

            // user id
            $userID = $result['id'];
        }else{
            // Insert user data
            $data['created'] = date("Y-m-d H:i:s");
            $data['modified'] = date("Y-m-d H:i:s");
            $insert = $this->db->insert($this->tableName,$data);

            // user id
            $userID = $this->db->insert_id();
        }

        // Return user id
        return $userID?$userID:false;
    }

}

I included this in config/autoload.php

$autoload['helper'] = array('url');

What happens is that I am redirected to the login page. Profile data isn't displayed and sometimes nothing is saved in the database. What I want is that after authentication be redirected to the profile page. If I try to access to the login page, be redirected to the profile page until I logout.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问