Frontend login
You can access Kirby's login logic from your templates and controllers, which allows you to use all of Kirby's login methods for frontend login forms.
Kirby's Auth class provides the following methods that can be used for authentication. All methods will throw exceptions if the input is not valid.
Logging in
$result = $kirby->auth()->authenticate(string $method, string|null $email, string|null $password = null, bool $long = false);
$method is one of the enabled login methods, e.g. password or code. If the $long parameter is set to true, the user will stay logged in using a "long" session (default = false).
If the login is complete, authenticate() logs the user in and returns the user. If another step is needed, e.g. the code for a login via code or a second factor, it returns a status object with a pending challenge instead:
use Kirby\Auth\State;
use Kirby\Auth\Status;
$result = $kirby->auth()->authenticate('password', $email, $password);
if ($result instanceof Status && $result->is(State::Pending)) {
// show the form for the code
}
You can find an example how to build a login form in our cookbook recipe "Restricting access to your site".
Email and password only
$kirby->auth()->login(string $email, string $password, bool $long = false);
This method validates the email and password of the user and logs the user in if the credentials are correct. It only works if password is one of your login methods. For users who have set up a second factor, it starts their challenge and then throws a Kirby\Auth\Exception\LoginNotPermittedException. Use authenticate() to support second factors.
Creating an authentication challenge
$status = $kirby->auth()->createChallenge(string $email, bool $long = false, string $mode = 'login');
This method can be used for passwordless login or password reset.
It creates an authentication challenge (for example by sending an email with a login code). The type of challenge that gets created is determined automatically based on the user's email address and the provided $mode (which can be login or password-reset). The enabled challenges are respected.
The method returns the authentication status object. This object contains all necessary information about the next steps:
$status->challenge(); // for example 'email', 'totp', ...
$status->email(); // email address of the pending authentication
$status->state(); // e.g. State::Pending if a challenge is active
$status->toArray(); // all public information combined in an array
state() returns one of the cases of the Kirby\Auth\State enum: Active, Impersonated, Pending or Inactive. To check for one of them, use $status->is(State::Pending).
For security, the status object with a pending challenge is also returned if no challenge was available for the user (e.g. if the user doesn't exist or no suitable challenge was found). This is because Kirby would otherwise leak which users exist and which don't, which is a piece of information that could be used by attackers. With auth.debug set to true, an exception will be thrown in this case, but in production it's important to keep this information secret.
If your code needs to know if a challenge was really created and you know what you are doing, you can override this security feature by calling $status->challenge(false).
Kirby remembers the pending authentication status via the user's session. You can access the status at any time with $kirby->auth()->status().
Verifying a provided code
Once the user enters the code you requested with authenticate() or createChallenge(), all you need to do is to call the $auth->verifyChallenge() method and Kirby will automatically check if the code is correct:
$kirby->auth()->verifyChallenge(string $code);