`I’m encountering an error in my CodeIgniter 4.5.4 application while using CodeIgniter Shield for authentication. The error message is:
Call to undefined method CodeIgniterShieldAuthenticationAuthentication::loggedIn()
It seems like loggedIn() is not a method available in the CodeIgniterShieldAuthenticationAuthentication class. According to the documentation I reviewed, the correct method to check authentication status might be different.
Questions:
What is the correct method to check if a user is logged in using CodeIgniter Shield?
How should I modify my code to replace the loggedIn() method with the appropriate method?
Any help or guidance on how to resolve this issue would be greatly appreciated!`
Here’s a snippet of my Home controller:
<?php
namespace AppControllers;
use PsrHttpClientClientExceptionInterface;
use CodeIgniterController;
use CodeIgniterShieldAuthenticationAuthentication;
class Home extends BaseController
{
public function index()
{
// Get the Shield authentication instance
$auth = service('authentication');
// Check if the user is logged in
if (!$auth->isLoggedIn()) {
// If not logged in, redirect to the login page
return redirect()->to('login');
}
// If the user is logged in, proceed with fetching GitHub repository details
try {
$repos = $this->github->getRepos();
// Prepare data for the view
$data = [
'pageTitle' => 'Welcome to News Hub',
'html_url' => $repos['framework4']->html_url,
'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count),
'forks_count' => number_format($repos['codeigniter4']->forks_count),
];
} catch (ClientExceptionInterface $e) {
// Log error and prepare fallback data
log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage());
$data = [
'pageTitle' => 'Welcome to News Hub',
'html_url' => 'https://github.com/codeigniter4/CodeIgniter4',
'stargazers_count' => '',
'forks_count' => '',
];
} catch (Exception $e) {
// Handle any other exceptions
log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage());
$data = [
'pageTitle' => 'Welcome to News Hub',
'html_url' => 'https://github.com/codeigniter4/CodeIgniter4',
'stargazers_count' => '',
'forks_count' => '',
];
}
// Render the view with the fetched data
return view('home', $data); // Replace 'home' with your desired view
}
}
1