delight-im/auth
delight-im/auth is a framework-agnostic PHP authentication library with a simple API. It handles registration, login, email verification, password resets, sessions, cookies, roles, and social login, with no complex setup required.
src/Auth.php, src/Database.php, and the SQL schema files under database/ (mysql.sql, pgsql.sql, sqlite.sql). Almost everything you need is on Delight\Auth\Auth.composer require delight-im/auth.Delight\Auth\Auth as a singleton, but resolve it lazily. Do not instantiate it in a global bootstrapper before Laravel’s session middleware has run.// app/Providers/PhpAuthServiceProvider.php
use Delight\Auth\Auth;
use Illuminate\Support\ServiceProvider;
public function register(): void
{
$this->app->singleton(Auth::class, function ($app) {
return new Auth($app['db']->connection()->getPdo(), [
'db_table_prefix' => 'php_auth_',
'session_name' => 'php_auth_session',
'cookie_name' => 'php_auth_remember',
'throttling' => true,
]);
});
}
$auth->login($email, $password, $remember), catch the package’s specific exceptions, then redirect. Protect routes with middleware that checks $auth->isLoggedIn().try {
$auth->login($request->email, $request->password, $request->boolean('remember'));
return redirect()->intended('/dashboard');
} catch (\Delight\Auth\InvalidEmailException|\Delight\Auth\InvalidPasswordException $e) {
return back()->withErrors(['email' => 'Invalid credentials.']);
} catch (\Delight\Auth\EmailNotVerifiedException $e) {
return back()->withErrors(['email' => 'Please verify your email address.']);
} catch (\Delight\Auth\TooManyRequestsException $e) {
return back()->withErrors(['email' => 'Too many attempts. Try again later.']);
}
Delight\Auth\Auth into controllers and middleware. Keep the PDO source explicit via DB::connection()->getPdo() or a dedicated connection if you want isolation.register() accepts a callback receiving $selector and $token. Build a Laravel route URL there and queue a Mailable. Do not send mail synchronously inside the callback.$userId = $auth->register($email, $password, $username, function ($selector, $token) use ($email) {
$url = route('auth.verify', ['selector' => $selector, 'token' => $token]);
Mail::to($email)->queue(new VerifyEmail($url));
});
$auth->forgotPassword($email, $callback) to generate the reset selector/token, then $auth->resetPassword($selector, $token, $newPassword) in the reset action. Catch InvalidSelectorTokenPairException and TokenExpiredException.auth()->user(). If you keep it as the source of truth, share a small DTO with views from middleware or a view composer:View::share('authUser', $auth->isLoggedIn() ? [
'id' => $auth->getUserId(),
'email' => $auth->getEmail(),
] : null);
auth()->user(), use PHP-Auth for credential verification and token flows, then bridge on successful login. This only works cleanly if you have a compatible Eloquent user model or sync records. Clear PHP-Auth’s session afterward if you do not want dual sessions.$auth->login($email, $password, $remember);
$id = $auth->getUserId();
$auth->logout(); // clear PHP-Auth session/cookie after bridging
LaravelAuth::loginUsingId($id);
$auth->admin()->createRole('admin'), addRoleForUserById(), doesUserHaveRole(), and $auth->hasRole('admin') for the current user. Treat this as a bitmask role flag, not a full RBAC system; map to Laravel Gate/policies if you bridge.:memory: in tests, run the PHP-Auth SQLite schema in setUp(), and bind Delight\Auth\Auth to the test PDO. Assert the package exceptions directly rather than only asserting HTTP responses.session_name and cookie_name values. If Laravel’s StartSession middleware has already started the native session, PHP-Auth’s session handling can conflict. Resolve it only inside auth routes/controllers, or test a bridging pattern carefully.users table. PHP-Auth expects columns such as verified, roles_mask, status, registered, and last_login, which do not match Laravel’s default schema. Use a prefixed table set or a separate database.db_table_prefix changes runtime queries, but the SQL schema you run must create tables with the same names. Copy and edit the supplied SQL rather than assuming the package migrates for you.InvalidEmailException, InvalidPasswordException, UserAlreadyExistsException, EmailNotVerifiedException, TooManyRequestsException, InvalidSelectorTokenPairException, and TokenExpiredException. Avoid a blanket catch (\Exception) that hides throttling and verification failures.users_throttling. Behind proxies, ensure trusted proxy configuration so IP-based throttling sees the real client IP.password_hash, so hashes are generally compatible with Laravel’s Hash, but do not double-hash or migrate hashes casually. If you bridge, verify Laravel’s user provider is reading the same password column.web middleware and VerifyCsrfToken. PHP-Auth handles authentication, not CSRF protection.cookie_secure, cookie_same_site, session_cookie_secure, and sensible lifetimes in production. Remember-me cookies are long-lived.$_SESSION keys and cookie names, confirm the PDO driver matches the SQL dialect, and verify the table prefix. Most “login silently fails” issues are session/cookie collisions or schema mismatches.How can I help you explore Laravel packages today?