The authentication of crudadmin/helpers is a set of API routes. Apps sign users in with a password, an OTP code or a social account and receive a Sanctum token.

Guard

Sanctum is installed with CrudAdmin. Publish its migrations once:
Add a Sanctum guard for your users:
config/auth.php
The controllers look users up in the model of the default guard. Set $guard in your controller to use another guard, see Login, or set it for all controllers in the config, e.g. for e-shop clients:
config/admin_helpers.php
With auth.guard, the logged user is set into that guard instead of the guard of the model (getGuard()). The client model needs HasApiTokens (Sanctum) and should use HasUserAuth with protected $guard.

User model

Extend BaseAuthenticatable and use the HasUserAuth and HasApiTokens traits.
app/Models/User.php
  • setAuthResponse() is required. It selects the attributes of the user returned by the login, registration and user routes.
  • phone is saved in the E.164 format, e.g. +421900123456.
  • verified is optional. It stores which e-mail addresses and phone numbers the user confirmed with an OTP code. Check it with $user->isVerified('email', $user->email).
HasUserAuth adds these methods:

Project helpers

The package calls these functions. Define them in an autoloaded file of your project:
app/Utilities/helpers.php
Without phoneValidatorSupportedCountries(), phone numbers are not formatted.

Routes

The AdminAuth facade registers the routes. Call it in your API routes file. Pass your own controller, or leave the argument out to use the default one.
routes/api.php
FixPhoneNumber formats the phone parameter of every request to E.164, so users can type the number in any format. The routes have no names. Register user, logout and changeIdentifier inside an authenticated group.

Rate limiting

Routes are limited per user or IP address and URL. Change the limits in the config:
config/admin_helpers.php
Apply the same limits to your own routes:
routes/api.php

Responses

Every route answers with an autoAjax JSON response. Errors have error: true, the message and the HTTP status, e.g. 401 for wrong credentials or 422 for validation errors.

Events

AdminHelpers\Auth\Events\UserRegistered is dispatched when a new user finishes the registration. Its $user property holds the user.