Getting StartedInstallation
Getting Started

Installation

On this page

Introduction

The Quick Start gives you the short version. This page covers the details you'll want as you set things up.

Requirements

  • PHP 8.5 or later
  • Laravel 13.31 or later
  • Inertia v3 (optional). Without Inertia, Keyring renders Blade views instead. See Pages.
  • The sodium PHP extension, if you want passkeys and security keys that sign with Ed25519. Without it, Keyring only offers ES256 keys by default.

Installing Keyring

To get started, install the package with Composer, then run the keyring:install Artisan command and your migrations:

Terminal
composer require keyring/keyring php artisan keyring:installphp artisan migrate

The keyring:install Artisan command asks you three questions:

  1. Which sign-in methods besides the password? None by default. A new app starts with the password, and the other methods stay in your provider, commented out, so you can add them later. Recovery codes come with any of them. Email codes are listed as what they are: an extra step at sign in, not a second factor. A password and an emailed code is still AAL1 (see Email Codes), and the provider says so above their line.
  2. Require a second factor for everyone? This question is asked when you pick an authenticator app, passkeys, or security keys, and the answer is yes by default. Users can still sign in with what they have, but every page then sends them to /settings/security until they've set one up (see Requiring a Second Factor). The question isn't asked with the password alone or with only email codes. Nobody could meet the requirement in those cases, so nothing requires a second factor.
  3. Which features? By default, you get registration, password resets, email verification, changing the password, and the security log. If you picked a sign-in method, you also get managing sign-in methods. Registration always brings email verification with it, because by default new users sign in once they've verified their address.

Without prompts (--no-interaction, or in CI), you get the same defaults, which means the password alone. You may also answer the questions with options:

Terminal
php artisan keyring:install --factor=totp --factor=passkeys --feature=registration --feature=manage-authenticators --require-second-factor
Option Values
--factor totp, passkeys, security-keys, or email-codes. none keeps the password alone without asking.
--feature registration, reset-passwords, email-verification, update-passwords, manage-authenticators, devices, email-changes, or security-log.
--require-second-factor, --optional-second-factor Answer the second question.

The --feature option replaces the defaults (registration, reset-passwords, email-verification, update-passwords, and security-log), so list those too if you want them. An unknown value stops the command before it writes anything, and so does requiring a second factor without a sign-in method that counts.

If you need to overwrite files that already exist, pass --force. Without it, existing files are skipped. And when config/keyring.php is already there, the command doesn't ask which features to turn on.

The command is safe to run again. A migration you already have is never added a second time under a new timestamp, and a migration that a later version brings is added on its own. Running it again on an older install also adds any new actions, but it leaves your provider alone, so you'll need to register those actions there yourself. Until you do, a new action's routes fail with an exception that says which Keyring::*Using() call is missing. A new email needs nothing from you. Keyring's emails are its own, so an email that a later version brings is sent from the start.

If you're moving from Fortify, read Upgrading From Fortify before you migrate.

What Gets Published

The keyring:install command publishes the following files:

File What It Is
config/keyring.php A short config file with the guard, username, home, your features, how registration answers, your enrollment page, and your support contact. Every other option keeps Keyring's default. You may copy any option from Configuration to change it.
database/migrations/*_create_keyring_*_table.php Seven tables: keyring_authenticators, keyring_devices, keyring_sessions, keyring_pending_changes, keyring_events, keyring_failure_counts, and keyring_password_holds. They're all created now, so you won't need a new migration to turn on a feature later. Tables for features you don't use stay empty.
database/migrations/*_add_origin_to_keyring_sessions_table.php Adds the origin column to keyring_sessions, which lets a session keep its handle when it moves to a new record.
database/migrations/*_add_announcements_to_keyring_sessions_table.php Adds announced_at, end_reason, announce_attempts, and announce_after to keyring_sessions, which let keyring:sweep announce every session that ends.
app/Providers/KeyringServiceProvider.php Where you register sign-in methods, the policy, the risk signals, and your actions. It lists every sign-in method, with the ones you didn't pick commented out. It does the same for the keyring.enrolled middleware, which requires a second factor, and for the risk signals that your features can't feed yet. To add one later, uncomment its line. The provider is added to bootstrap/providers.php for you.
app/Actions/Keyring/*.php Your code for the features that need it. All of these are published and registered, so a feature works as soon as you turn it on: registration (CreateNewUser), password resets (ResetUserPassword), password changes (UpdateUserPassword), email change requests (ValidateEmailChange), and the password rules they share (PasswordValidationRules). Keyring does the security work around them.

Keyring's emails aren't published. They're Keyring's own and are sent from the start. To change one, give Keyring::notifications() your own class for it.

You may also publish individual files with the keyring-config, keyring-migrations, and keyring-lang tags. The keyring-config tag publishes the full config file, with every option and its default:

Terminal
php artisan vendor:publish --tag=keyring-config

The migrations use morphs() to link rows to users. If your users have UUID or ULID keys, set Laravel's default morph key type before you run the migrations. For example, you may call Schema::morphUsingUlids(). You may also edit the published migrations instead.

Preparing Your User Model

Add the HasKeyring trait to your user model:

PHP
use Illuminate\Contracts\Auth\MustVerifyEmail;use Illuminate\Foundation\Auth\User as Authenticatable;use Keyring\Concerns\HasKeyring; class User extends Authenticatable implements MustVerifyEmail{    use HasKeyring;     protected function casts(): array    {        return [            'email_verified_at' => 'datetime',            'password' => 'hashed',        ];    }}

The HasKeyring trait does three things for you:

  • It adds $user->keyring, $user->authenticators(), and $user->devices().
  • Once Keyring handles changes to the password and email attributes, which happens when you enable Features::updatePasswords() and Features::emailChanges(), the trait stops those attributes from changing outside Keyring's own flows. See Protected Changes.
  • When you delete a user, the trait revokes their sessions, ends their reset links, and deletes their authenticators, devices, and pending changes. With soft deletes, the sessions and reset links end straight away, along with "remember me" cookies, device trust, and any pending email change. Everything else waits for a force delete. A restored user keeps their sign-in methods and signs in again.

The published actions hash the password themselves, so they work with or without the hashed cast shown above. When you use the cast, a password that's already hashed isn't hashed again.

MustVerifyEmail and Features::emailVerification() go together. If your model has the interface but the feature is off, registering throws an exception that says so, before anything is saved. Registration needs both, because a new user is signed out until they've verified their address: the application won't boot with registration on and the feature off, and registering throws without the interface. The exception is registration.reveal_taken, which needs neither. If your users implement MustVerifyEmail, they must also verify their address before they can add a sign-in method. See Managing Authenticators.

By default, Keyring looks users up by the email attribute. You may change this with username (see Configuration). Lookups trim and lowercase the input, so store addresses the same way. The Keyring::normalizeUsername method does exactly that, and the published CreateNewUser action uses it. To normalise differently, see Finding the User.

Frontend

If you plan to offer passkeys or security keys, install @keyringjs/keyring, Keyring's browser package. It also has helpers for the challenge page:

Terminal
npm install @keyringjs/keyring

See Frontend for what the package provides.

Routes Keyring Registers

By default, Keyring registers its routes under the web middleware group, and every signed-in route checks Keyring's guard (auth:{guard}). Some of the routes use the same names as Laravel's conventions, such as login, logout, register, password.request, password.reset, verification.notice, and so on. If you have routes of your own with those names, remove them. You can find the full list in The Sign In Flow, Step-Up, Managing Authenticators, Devices & Sessions, and Protected Changes.

If you want to change the prefix, domain, or middleware, or turn the routes off, see Configuration.

After Installing

  • Set APP_URL to the address your users actually visit. Keyring builds emailed links from it. Passkeys and security keys only work on that origin, unless you configure others. See Passkeys & Security Keys.
  • Build your pages. If you required a second factor, you'll also need the page at enrollment_url.
  • If your queue isn't sync, run a queue worker. Security emails are queued, so they wait for the worker, while sign-in codes go out straight away.
  • You may add features and sign-in methods as you need them.
  • Run Laravel's scheduler (* * * * * php artisan schedule:run) if you don't already. Keyring adds keyring:prune to the schedule, and it runs daily. That command clears out old devices, finished email changes, old security log entries, and ended sessions. See Pruning.
  • Sessions end after two hours of inactivity, whatever session.lifetime says, unless the browser ticked "remember me". To change that, set sessions.idle. See Timeouts.
  • If you have signed-in routes outside the web group, they need the keyring.session middleware. Otherwise, a revoked or timed-out session keeps working on those routes. See The Session Middleware.
  • To hear about every session that ends, including ones that time out with no request, set sessions.sweep_every. Keyring then schedules keyring:sweep. See Sweeping Ended Sessions.
  • The keyring:sign-out Artisan command signs one user out everywhere. Pass --all to sign everyone out instead. It's useful for a stolen laptop, or for an account that someone else got into. See For Administrators.

keyring:install ends by listing a few things to check before going live: the idle timeout, a bcrypt note when bcrypt is the default hasher, the scheduler, the keyring.session middleware, and keyring:sign-out.