appoly/api-password-helper
Laravel: API forgotten password helper
Introduction
Adds two API routes for "forgot password" with an emailed numeric code, plus a password_helper_key column on users.
Requires PHP 8.3+ and Laravel 12 or 13. Upgrading from 2.x? See Upgrading from 2.x.
Install
composer require appoly/api-password-helper
php artisan migrate
The user model must be Notifiable:
class User extends Authenticatable
{
use Notifiable;
password_helper_key does not need to be in $fillable.
Routes
GET /api/forgot-password?email=johndoe@example.com
POST /api/forgot-password key=123456&password=new-password[&email=johndoe@example.com]
| Response | Status | Body |
|---|---|---|
| Code sent | 200 | {"success":true,"message":"An email has been sent to your account"} |
| Password updated | 200 | {"success":true,"message":"Your password has been updated"} |
| Unknown email, or wrong / expired / used code | 400 | {"success":false,"message":"No user found"} |
GET without email |
400 | {"success":false,"message":"Email is required"} |
POST without key or password |
400 | {"success":false,"message":"Key and Password are required"} |
| Rate limited | 429 | {"success":false,"message":"Too many attempts, please try again later"} |
To replace a route, register the same method and URI in your app (e.g. routes/api.php); the later route wins.
Security behaviour
- Codes come from
random_int, are unique among outstanding codes, and expire afterPASSWORD_RESET_EXPIRY_MINUTES(default 60). - Only an HMAC of the code is stored (
{hmac}:{expiresAt}), keyed onAPP_KEY. RotatingAPP_KEYcancels outstanding codes. - Requests are rate limited (named limiters
appoly-password-forgotandappoly-password-reset), and failed resets are capped app-wide per hour. Limits use the default cache store, so it must be shared between servers. - Per-IP limits use
$request->ip(), like Laravel's ownthrottlemiddleware. If the app sits behind Cloudflare or a load balancer, configure trusted proxies, or every user shares the proxy's IP and one limit. With no proxy in front, leave trusted proxies off (the Laravel default). - If the POST includes
email, the code must belong to that user. SetPASSWORD_RESET_REQUIRE_EMAILtotrueto makeemailmandatory; this is the strongest setting, because it caps guesses per account. - On success the package fires
Illuminate\Auth\Events\PasswordReset. It doesn't revoke existing tokens; listen for the event to do that:
Event::listen(PasswordReset::class, fn (PasswordReset $event) => $event->user->tokens()->delete());
Configuration
php artisan vendor:publish --tag=api-password-helper-config
| Key | Default | |
|---|---|---|
PASSWORD_RESET_CODE_LENGTH |
6 |
Digits in the code (1–18) |
PASSWORD_RESET_SUBJECT, _GREETING, _BEFORE_CODE, _AFTER_CODE, _SIGN_OFF |
Email text | |
PASSWORD_RESET_DEEPLINK |
'' |
If set, the email shows a button linking to this URL with the code appended |
PASSWORD_RESET_EXPIRY_MINUTES |
60 |
|
PASSWORD_RESET_REQUIRE_EMAIL |
false |
|
PASSWORD_RESET_MAX_ATTEMPTS |
5 |
Wrong codes per user before their code is cleared (only counted when email is sent) |
PASSWORD_RESET_RATE_LIMITS.forgot |
5 |
Per minute, per IP and per email. null disables |
PASSWORD_RESET_RATE_LIMITS.reset |
5 |
Per minute, per IP. null disables |
PASSWORD_RESET_RATE_LIMITS.reset_global_failures |
100 |
Failed resets per hour, app-wide. null disables |
PASSWORD_RESET_RULES |
null |
Validation rules for the new password, e.g. ['min:8'] |
Older published configs work; missing keys use the defaults above.
Optional index
Lookups use a prefix match on password_helper_key. On large users tables, add an index:
php artisan vendor:publish --tag=api-password-helper-index-migration
php artisan migrate
It isn't run automatically because indexing a large table may be slow. On PostgreSQL a plain index won't serve LIKE 'prefix%' unless the column uses the C collation; use a text_pattern_ops index instead.
Custom notification text
ResetPassword takes ($user, string $code) and exposes the plain code as $notification->code.
Upgrading from 2.x
The request and response JSON are unchanged, apart from a new 429 response. Mobile apps need no changes.
Required
-
Require PHP 8.3+ and Laravel 12 or 13.
-
Run
composer require appoly/api-password-helper:^3.0. A^2.xconstraint won't pick up 3.0. -
Fix tests that seed
password_helper_keywith a plain code or read the code from the database. The column now stores{hmac}:{expiresAt}. Get the code from the notification instead:Notification::fake(); $this->getJson('/api/forgot-password?email='.$user->email); Notification::assertSentTo($user, ResetPassword::class, function ($n) use (&$code) { $code = $n->code; return true; }); -
If you construct
ResetPasswordyourself, pass the code:new ResetPassword($user, $code). -
Remove any use of the
ApiPasswordsfacade alias. It was never bound, so it couldn't have worked.
Check before going live
- Codes requested before the deploy stop working. Users must request a new one.
- Codes expire after 60 minutes.
- Your app handles a
429sensibly. - Only if the app sits behind Cloudflare or a load balancer: configure trusted proxies. Otherwise every user shares the proxy's IP and one rate limit of 5 per minute. With no proxy in front, nothing to do.
- An existing
PasswordResetlistener in your app will now also run for API resets. - If your app already sends
emailon the reset POST, the code must now belong to that email.
Optional
- Listen for
PasswordResetto revoke tokens on other devices (see Security behaviour). - Publish the index migration.
- Set
PASSWORD_RESET_REQUIRE_EMAIL=trueonce every client sendsemail. - Remove
password_helper_keyfrom$fillable.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 3.0.0 | ^8.3 | ^12.0| | MIT | |
| 2.3.3 | MIT | |||
| 2.3.2 | MIT |