Compare commits
1 Commits
feature/Fi
...
feature/Fo
| Author | SHA1 | Date | |
|---|---|---|---|
| 99d24c38c7 |
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Services\Auth\PasswordResetOtpService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ForgotPasswordOtpController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PasswordResetOtpService $otpService
|
||||
) {}
|
||||
|
||||
public function sendOtp(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:users,email'],
|
||||
]);
|
||||
|
||||
$email = $validated['email'];
|
||||
|
||||
$key = 'user-forgot-password-otp:' . $email . ':' . $request->ip();
|
||||
|
||||
if (RateLimiter::tooManyAttempts($key, 3)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['Too many OTP requests. Please try again later.'],
|
||||
]);
|
||||
}
|
||||
|
||||
RateLimiter::hit($key, 600);
|
||||
|
||||
$user = User::query()->where('email', $email)->firstOrFail();
|
||||
|
||||
$this->otpService->sendOtp($user, 'user');
|
||||
|
||||
return response()->json([
|
||||
'message' => 'OTP sent to your email.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function verifyOtp(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:users,email'],
|
||||
'otp' => ['required', 'digits:6'],
|
||||
]);
|
||||
|
||||
$this->otpService->verifyOtp(
|
||||
email: $validated['email'],
|
||||
otp: $validated['otp'],
|
||||
guard: 'user'
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'OTP verified successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function resetPassword(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:users,email'],
|
||||
'password' => ['required', 'confirmed',],
|
||||
]);
|
||||
|
||||
$record = $this->otpService->ensureVerified(
|
||||
email: $validated['email'],
|
||||
guard: 'user'
|
||||
);
|
||||
|
||||
$user = User::query()->where('email', $validated['email'])->firstOrFail();
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
$record->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'User password reset successfully.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
10
backend/app/Models/PasswordResetOtp.php
Normal file
10
backend/app/Models/PasswordResetOtp.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PasswordResetOtp extends Model
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
}
|
||||
32
backend/app/Notifications/ResetPasswordOtpNotification.php
Normal file
32
backend/app/Notifications/ResetPasswordOtpNotification.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class ResetPasswordOtpNotification extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $otp
|
||||
) {}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
return (new MailMessage)
|
||||
->subject('Reset Password OTP')
|
||||
->greeting('Hello!')
|
||||
->line('Your password reset code is:')
|
||||
->line($this->otp)
|
||||
->line('This code will expire in 10 minutes.')
|
||||
->line('If you did not request password reset, ignore this email.');
|
||||
}
|
||||
}
|
||||
103
backend/app/Services/Auth/PasswordResetOtpService.php
Normal file
103
backend/app/Services/Auth/PasswordResetOtpService.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\PasswordResetOtp;
|
||||
use App\Notifications\ResetPasswordOtpNotification;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class PasswordResetOtpService
|
||||
{
|
||||
public function sendOtp(Model $user, string $guard = 'user'): void
|
||||
{
|
||||
$otp = (string) random_int(100000, 999999);
|
||||
|
||||
PasswordResetOtp::query()->where('email', $user->email)
|
||||
->where('guard', $guard)
|
||||
->delete();
|
||||
|
||||
PasswordResetOtp::query()->create([
|
||||
'email' => $user->email,
|
||||
'guard' => $guard,
|
||||
'otp' => Hash::make($otp),
|
||||
'expires_at' => now()->addMinutes(10),
|
||||
]);
|
||||
|
||||
$user->notify(new ResetPasswordOtpNotification($otp));
|
||||
}
|
||||
|
||||
public function verifyOtp(string $email, string $otp, string $guard = 'user'): void
|
||||
{
|
||||
$record = PasswordResetOtp::query()->where('email', $email)
|
||||
->where('guard', $guard)
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if (! $record) {
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Invalid OTP.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($record->isExpired()) {
|
||||
$record->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['OTP has expired.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($record->attempts >= 5) {
|
||||
$record->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Too many wrong attempts. Please request a new OTP.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if (! Hash::check($otp, $record->otp)) {
|
||||
$record->increment('attempts');
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Invalid OTP.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$record->update([
|
||||
'verified_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function ensureVerified(string $email, string $guard = 'user'): PasswordResetOtp
|
||||
{
|
||||
$record = PasswordResetOtp::query()->where('email', $email)
|
||||
->where('guard', $guard)
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if (! $record || ! $record->isVerified()) {
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['Please verify OTP first.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($record->isExpired()) {
|
||||
$record->delete();
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'otp' => ['OTP has expired.'],
|
||||
]);
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
public function deleteOtp(string $email, string $guard = 'user'): void
|
||||
{
|
||||
PasswordResetOtp::query()->where('email', $email)
|
||||
->where('guard', $guard)
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
89
backend/app/User/Controllers/ForgotPasswordOtpController.php
Normal file
89
backend/app/User/Controllers/ForgotPasswordOtpController.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Expert;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Services\Auth\PasswordResetOtpService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ForgotPasswordOtpController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PasswordResetOtpService $otpService
|
||||
) {}
|
||||
|
||||
public function sendOtp(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:experts,email'],
|
||||
]);
|
||||
|
||||
$email = $validated['email'];
|
||||
|
||||
$key = 'expert-forgot-password-otp:' . $email . ':' . $request->ip();
|
||||
|
||||
if (RateLimiter::tooManyAttempts($key, 3)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['Too many OTP requests. Please try again later.'],
|
||||
]);
|
||||
}
|
||||
|
||||
RateLimiter::hit($key, 600);
|
||||
|
||||
$expert = Expert::query()->where('email', $email)->firstOrFail();
|
||||
|
||||
$this->otpService->sendOtp($expert, 'expert');
|
||||
|
||||
return response()->json([
|
||||
'message' => 'OTP sent to your email.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function verifyOtp(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:experts,email'],
|
||||
'otp' => ['required', 'digits:6'],
|
||||
]);
|
||||
|
||||
$this->otpService->verifyOtp(
|
||||
email: $validated['email'],
|
||||
otp: $validated['otp'],
|
||||
guard: 'expert'
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'OTP verified successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function resetPassword(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'exists:experts,email'],
|
||||
'password' => ['required', 'confirmed'],
|
||||
]);
|
||||
|
||||
$record = $this->otpService->ensureVerified(
|
||||
email: $validated['email'],
|
||||
guard: 'expert'
|
||||
);
|
||||
|
||||
$expert = Expert::query()->where('email', $validated['email'])->firstOrFail();
|
||||
|
||||
$expert->update([
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
$record->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Expert password reset successfully.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('password_reset_otps', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('email')->index();
|
||||
$table->string('otp');
|
||||
$table->string('guard_name');
|
||||
$table->timestamp('expires_at');
|
||||
$table->timestamp('verified_at')->nullable();
|
||||
$table->unsignedTinyInteger('attempts')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('password_reset_otps');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user