Compare commits
5 Commits
feature/Fo
...
4eec548b83
| Author | SHA1 | Date | |
|---|---|---|---|
| 4eec548b83 | |||
| 675d2bdbad | |||
| 69ffe322ce | |||
| a1466d8f57 | |||
| 392fca8635 |
@@ -1,89 +0,0 @@
|
||||
<?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.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class PasswordResetOtp extends Model
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
<?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.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Passport\ClientRepository;
|
||||
use Throwable;
|
||||
|
||||
class GatewayManagementController extends Controller
|
||||
@@ -27,7 +28,7 @@ class GatewayManagementController extends Controller
|
||||
$request,
|
||||
allowedFilters: ['*'],
|
||||
allowedSortings: ['*'],
|
||||
allowedSelects: ['id', 'name', 'domain', 'tax_id', 'bank_name', 'account_holder', 'iban', 'business_type_id', 'business_type_name'],
|
||||
allowedSelects: ['*'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,9 +39,15 @@ class GatewayManagementController extends Controller
|
||||
{
|
||||
$user = Auth::guard('web')->user();
|
||||
|
||||
$logo = $request->has('logo') ? FileFacade::save($request->file('logo'), "{$user->id}") : null;
|
||||
$logo = $request->hasFile('logo')
|
||||
? FileFacade::save($request->file('logo'), "{$user->id}")
|
||||
: null;
|
||||
|
||||
$passportClient = DB::transaction(function () use ($user, $logo, $request) {
|
||||
|
||||
$clientName = "Gateway {$request->name} - User {$user->id}";
|
||||
$client = app(ClientRepository::class)->createPasswordGrantClient($clientName, 'users');
|
||||
|
||||
DB::transaction(function () use ($user, $logo, $request) {
|
||||
UserGateway::query()->create([
|
||||
'user_id' => $user->id,
|
||||
'username' => $user->username,
|
||||
@@ -53,15 +60,20 @@ class GatewayManagementController extends Controller
|
||||
'business_type_id' => $request->business_type_id,
|
||||
'business_type_name' => BusinessTypeEnum::name($request->business_type_id),
|
||||
'logo' => $logo,
|
||||
'client_id' => $client->id,
|
||||
'client_secret' => $client->secret,
|
||||
]);
|
||||
|
||||
$user->update(['authority_level' => 1]);
|
||||
|
||||
return $client;
|
||||
});
|
||||
|
||||
|
||||
return $this->successResponse();
|
||||
return $this->successResponse([
|
||||
'client_id' => $passportClient->id,
|
||||
'client_secret' => $passportClient->secret,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(UserGateway $gateway): JsonResponse
|
||||
{
|
||||
return $this->successResponse($gateway);
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
<?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');
|
||||
}
|
||||
};
|
||||
@@ -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::table('user_gateways', function (Blueprint $table) {
|
||||
$table->string('client_id')->nullable();
|
||||
$table->string('client_secret')->nullable();
|
||||
$table->integer('status')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('user_gateways', function (Blueprint $table) {
|
||||
$table->dropColumn(['client_id', 'client_secret','status']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -77,7 +77,7 @@ Route::prefix('expert')
|
||||
|
||||
Route::prefix('profile')
|
||||
->name('profile.')
|
||||
->middleware('auth')
|
||||
->middleware('auth:expert')
|
||||
->controller(ProfileController::class)
|
||||
->group(function () {
|
||||
Route::get('info', 'info')->name('info');
|
||||
|
||||
Reference in New Issue
Block a user