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:config/auth.php
$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
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
ExtendBaseAuthenticatable 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 anduserroutes.phoneis saved in the E.164 format, e.g.+421900123456.verifiedis 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
phoneValidatorSupportedCountries(), phone numbers are not formatted.
Routes
TheAdminAuth 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
routes/api.php
Responses
Every route answers with an autoAjax JSON response. Errors haveerror: 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.