Я новичок в codeigniter и пытаюсь настроить тест основного входа в Google с помощью OAuth. Это в основном одна страница с кнопкой входа в систему, и если я нажимаю такую кнопку, я перехожу на экран аутентификации OAuth. После аутентификации я ожидаю, что он перенаправит меня на страницу профиля. Страница профиля считывает данные из базы данных после того, как они были сохранены в аутентификации.
Я следовал этому уроку: https://www.codexworld.com/login-with-google-account-in-codeigniter/
Также я нашел этот вопрос: Перенаправление в codeigniter после входа в систему
Это точно такая же проблема, но она не решена, и я тоже не понимаю ответ.
Вот код:
/ Контроллеры / 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/');
}
}
/ модель / 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;
}
}
Я включил это в config / autoload.php
$autoload['helper'] = array('url');
Что происходит, так это то, что я перенаправлен на страницу входа. Данные профиля не отображаются и иногда ничего не сохраняется в базе данных. Что я хочу, чтобы после аутентификации быть перенаправлены на страницу профиля. Если я пытаюсь получить доступ к странице входа, меня будут перенаправлять на страницу профиля, пока я не выйду из системы.