Identity and accounts

Atom's Identity API answers a question application code often needs to ask: who is signed in on this request? Your application supplies the user model and the code that loads users. Atom's authenticator uses that code to recover the signed-in identity from the session.

Identity and the Accounts module

Part Responsibility
IdentityInterface Gives an authenticatable object a stable identifier through identifier().
IdentityProviderInterface Adapts your user store. It finds an identity by ID or login and validates credentials.
AuthenticatorInterface Reads the current identity and manages sign-in and sign-out.
Accounts module Provides account pages for workflows such as sign-in, registration, and password recovery, using the same Identity API.

The session stores the identity's identifier. When the authenticator first resolves it on a request, the provider loads the current user object. If that user no longer exists, Atom clears the stored identifier.

Get the current user

Inject Atom\Identity\AuthenticatorInterface into a service, controller, or page. Call identity() to get the current identity. A guest gets null.

use Atom\Identity\AuthenticatorInterface;

final readonly class CurrentUser
{
    public function __construct(private AuthenticatorInterface $auth)
    {
    }

    public function identifier(): string|int|null
    {
        return $this->auth->identity()?->identifier();
    }
}

The interface guarantees an identifier. To read an application-specific field, check the concrete identity type first. For example, if your provider returns App\Models\User and that class exposes name:

use App\Models\User;

$identity = $auth->identity();
$name = $identity instanceof User ? $identity->name : null;

Use $auth->check() when you only need to know whether someone is signed in, or $auth->guest() for the guest check.

Connect your user store

Bind IdentityProviderInterface to your application's adapter in a service provider. The Accounts module uses that same binding. Its setup guide explains the additional account-management contract for registration and password recovery.

use Atom\Identity\IdentityProviderInterface;

$bindings->bind(IdentityProviderInterface::class)->to(UserIdentityProvider::class);

When you implement credential checks, inject PasswordHasherInterface to verify stored password hashes. Atom's default hasher uses PHP's native password hashing functions.

Require a signed-in identity

Use AuthenticateMiddleware on a route that requires a signed-in user. A guest receives 401.

use Atom\Identity\AuthenticateMiddleware;
use Atom\Router\Route;

Route::get("/account", $handler)
    ->middleware(AuthenticateMiddleware::class);

See Sessions for session values and flash messages.