Authentication CI4 Shield
5 min read Quiz at the end
Add login, logout, and groups to CI4 apps using the official Shield authentication package.
Authentication with CI4 Shield
composer require codeigniter4/shield
php spark shield:setup
// Routes auto-registered
// GET /login — login form
// POST /login — authenticate
// GET /register — register form
// GET /logout — logout
// Get current user
$user = auth()->user();
$user = service("auth")->user();
// Check auth state
auth()->loggedIn();
// Protect routes
$routes->group("admin", ["filter" => "session"], function($r) {
$r->get("/dashboard", "AdminDashboard::index");
});
// Add to filter
public array $aliases = [
"session" => CodeIgniterShieldFiltersSessionAuth::class,
"tokens" => CodeIgniterShieldFiltersTokenAuth::class,
];
// Groups (roles)
$user->addToGroup("admin");
$user->inGroup("admin"); // true/false
Topic Quiz · 2 questions
Test your understanding before moving on
1. Which package provides authentication for CI4?
💡 CodeIgniter Shield is the official authentication package for CI4.
2. How do you check if a user is logged in with Shield?
💡 auth()->loggedIn() returns true if the current user is authenticated.