feat: add request verify
This commit is contained in:
@@ -453,7 +453,59 @@ public function checkOTP(Request $request)
|
|||||||
'token' => $token,
|
'token' => $token,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
public function requestMobileVerification(Request $request)
|
||||||
|
{
|
||||||
|
$data = $request->validate([
|
||||||
|
'mobile' => ['required', 'string', new MobileNumber],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mobile = MobileNumberHelper::formatMobile($data['mobile']);
|
||||||
|
|
||||||
|
if (User::where('mobile', $mobile)->where('id', '!=', auth()->id())->exists()) {
|
||||||
|
return response()->json(['message' => 'mobile already taken'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$otp = rand(10000, 99999);
|
||||||
|
|
||||||
|
auth()->user()->otpTokens()->create([
|
||||||
|
'token' => $otp
|
||||||
|
]);
|
||||||
|
|
||||||
|
SendOTPJob::dispatch($mobile, (string)$otp);
|
||||||
|
|
||||||
|
return response()->json(['message' => 'otp sent']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function confirmMobileVerification(Request $request)
|
||||||
|
{
|
||||||
|
$data = $request->validate([
|
||||||
|
'mobile' => ['required', 'string', new MobileNumber],
|
||||||
|
'token' => 'required|string'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mobile = MobileNumberHelper::formatMobile($data['mobile']);
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
$otp = $user->otpTokens()
|
||||||
|
->where('token', $data['token'])
|
||||||
|
->where('created_at', '>', now()->subMinutes(15))
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$otp) {
|
||||||
|
return response()->json(['message' => 'invalid token'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$otp->delete();
|
||||||
|
|
||||||
|
$user->update([
|
||||||
|
'mobile' => $mobile,
|
||||||
|
'mobile_verified_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'mobile verified successfully'
|
||||||
|
]);
|
||||||
|
}
|
||||||
public function updateProfile(Request $request)
|
public function updateProfile(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
|
|||||||
+7
-1
@@ -20,6 +20,9 @@ class User extends Authenticatable
|
|||||||
'female' => 2,
|
'female' => 2,
|
||||||
];
|
];
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
protected $casts = [
|
||||||
|
'mobile_verified_at' => 'datetime',
|
||||||
|
];
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
'password',
|
||||||
];
|
];
|
||||||
@@ -39,7 +42,10 @@ public function getFullNameAttribute()
|
|||||||
{
|
{
|
||||||
return $this->first_name . ' ' . $this->last_name;
|
return $this->first_name . ' ' . $this->last_name;
|
||||||
}
|
}
|
||||||
|
public function hasVerifiedMobile(): bool
|
||||||
|
{
|
||||||
|
return !is_null($this->mobile_verified_at);
|
||||||
|
}
|
||||||
public function getAvatarAttribute()
|
public function getAvatarAttribute()
|
||||||
{
|
{
|
||||||
return isset($this->attributes['avatar']) ? url($this->attributes['avatar']) : null;
|
return isset($this->attributes['avatar']) ? url($this->attributes['avatar']) : null;
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?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('users', function (Blueprint $table) {
|
||||||
|
$table->timestamp('mobile_verified_at')->nullable()->after('mobile');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('mobile_verified_at');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -55,6 +55,8 @@
|
|||||||
Route::apiResource('reminders', ReminderController::class);
|
Route::apiResource('reminders', ReminderController::class);
|
||||||
});
|
});
|
||||||
Route::middleware('auth:sanctum')->group(function () {
|
Route::middleware('auth:sanctum')->group(function () {
|
||||||
|
Route::post('/verify-mobile/request', [UserController::class, 'requestMobileVerification']);
|
||||||
|
Route::post('/verify-mobile/confirm', [UserController::class, 'confirmMobileVerification']);
|
||||||
Route::post('profile', [UserController::class, 'updateProfile']);
|
Route::post('profile', [UserController::class, 'updateProfile']);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user