laravel/sentinel
Laravel package providing Sentinel integration for authentication and authorization in Laravel apps. Adds user management, roles and permissions, login/registration flows, and easy setup for Cartalyst Sentinel-backed projects.
Installation:
composer require laravel/sentinel
php artisan sentinel:install
users, roles, permissions, throttle, etc.).Configuration:
php artisan vendor:publish --provider="Sentinel\SentinelServiceProvider" --tag="config"
config/sentinel.php to match your app’s needs (e.g., throttle settings, remember_token expiry).First Use Case:
use Sentinel\Sentinel;
$credentials = ['email' => 'user@example.com', 'password' => 'password123'];
$user = Sentinel::authenticate($credentials);
if ($user) {
// User is authenticated
return redirect()->intended('/dashboard');
}
if (Sentinel::check('edit_posts')) {
// User has permission
}
routes/web.php):
Route::get('/admin', function () {
// Admin-only content
})->middleware('auth:sentinel');
Create a User:
$user = Sentinel::registerAndActivate([
'email' => 'user@example.com',
'password' => 'password123',
'first_name' => 'John',
'last_name' => 'Doe',
]);
Assign Roles/Permissions:
$role = Sentinel::findRoleByName('admin');
$user->roles()->attach($role);
$permission = Sentinel::findPermissionByName('edit_posts');
$user->givePermissionTo($permission);
config/sentinel.php – Customize throttling, remember tokens, and other settings.database/migrations/ – Review Sentinel’s schema to avoid conflicts with existing tables.app/Http/Middleware/ – Check for custom auth middleware extending Sentinel’s functionality.app/Providers/EventServiceProvider.php – Listen to Sentinel events (e.g., auth.attempting, auth.failed) for custom logic.$user = Sentinel::authenticate($credentials);
if ($user) {
// Success
}
$user = Sentinel::authenticate($credentials, true); // Second param enables "remember me"
Sentinel::logout();
if (Sentinel::check('admin')) {
// Admin-only logic
}
$user->roles()->attach(Sentinel::findRoleByName('editor'));
if (Sentinel::check('publish_articles')) {
// User has permission
}
config/sentinel.php).if (Sentinel::throttler()->isThrottled($credentials['email'])) {
abort(429, 'Too many attempts. Try again later.');
}
$user = Sentinel::findById(1);
$user->password = 'new_secure_password';
$user->save();
Password facade or extend with custom logic.$token = Sentinel::personalAccessTokens()->create([
'name' => 'API Token',
'abilities' => ['read', 'write'],
]);
auth:sanctum or auth:api middleware alongside Sentinel for hybrid auth.$user = Sentinel::findById(1);
Sentinel::actingAs($user);
$this->actingAs($user);
$this->post('/login', $credentials)->assertSessionHasErrors();
Sentinel::registerAndActivate() for manual registration or extend with a form handler.MustVerifyEmail or custom logic to send verification emails.Route::group(['middleware' => ['auth:sentinel', 'role:admin']], function () {
// Admin-only routes
});
if (Sentinel::check('manage_users')) {
// Show user management UI
}
laravel-two-factor-auth alongside Sentinel.if (Sentinel::mfa()->verify($user, $code)) {
// MFA successful
}
laravel/socialite alongside Sentinel to handle social logins, then attach Sentinel roles/permissions post-login.$socialUser = Socialite::driver('github')->user();
$user = Sentinel::findByCredentials(['email' => $socialUser->email]);
if (!$user) {
$user = Sentinel::registerAndActivate([
'email' => $socialUser->email,
'password' => Str::random(16), // Dummy password
'first_name' => $socialUser->name,
]);
}
Sentinel::login($user);
Event::listen('auth.attempting', function ($credentials) {
Log::info('Login attempt for: ' . $credentials['email']);
});
throttle table by default.namespace App\Extensions;
use Sentinel\User;
class UserExtension {
public function extend(User $user) {
$user->macro('isPremium', function () {
return $this->roles()->where('name', 'premium')->exists();
});
}
}
Notification system.namespace App\Guards;
use Sentinel\Guards\Guard;
class ApiGuard extends Guard {
public function check() {
// Custom logic
}
}
Sentinel::extend('api', function ($app) {
return new ApiGuard();
});
config/sentinel.php to use Redis or another driver for throttling:
'throttle' => [
'driver' => 'redis',
'key' => 'sentinel.throttle',
],
$this->actingAs($user, 'sentinel');
$this->post('/login', $credentials)
->assertSessionHasErrors()
->assertStatus(429); // Throttled
How can I help you explore Laravel packages today?