doumanju2533 2019-04-21 20:25
浏览 38

如何在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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab数字图像处理频率域滤波
    • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
    • ¥15 ELGamal和paillier计算效率谁快?
    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
    • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
    • ¥15 Arcgis相交分析无法绘制一个或多个图形
    • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误