feat: password reset, email change with verification, and password change
Password reset: multi-app support with custom notification linking to correct frontend (app/portal/admin). Email change: self-service with password confirmation and admin-initiated, both sending verification to new address with 24h expiry. Confirmation sent to old email on completion. Password change: authenticated endpoint revoking other sessions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
55
api/app/Http/Controllers/Api/V1/AccountController.php
Normal file
55
api/app/Http/Controllers/Api/V1/AccountController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class AccountController extends Controller
|
||||
{
|
||||
/**
|
||||
* POST /api/v1/me/change-password
|
||||
* Authenticated user changes their own password.
|
||||
*/
|
||||
public function changePassword(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'current_password' => ['required'],
|
||||
'password' => ['required', 'confirmed', Password::min(8)->mixedCase()->numbers()],
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
if (! Hash::check($validated['current_password'], $user->password)) {
|
||||
throw ValidationException::withMessages([
|
||||
'current_password' => ['Het huidige wachtwoord is onjuist.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
// Revoke all OTHER tokens (keep current session)
|
||||
$currentToken = $user->currentAccessToken();
|
||||
if ($currentToken instanceof \Laravel\Sanctum\PersonalAccessToken) {
|
||||
$user->tokens()->where('id', '!=', $currentToken->id)->delete();
|
||||
} else {
|
||||
// TransientToken (test) or no token — revoke all
|
||||
$user->tokens()->delete();
|
||||
}
|
||||
|
||||
activity()
|
||||
->causedBy($user)
|
||||
->performedOn($user)
|
||||
->log('user.password_changed');
|
||||
|
||||
return $this->success(message: 'Je wachtwoord is succesvol gewijzigd.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user