diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 01c8545..27fe2f6 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -233,19 +233,19 @@ public function register(Request $request) // ✅ Check if email can receive a message before creating the user if (isset($data['email'])) { $code = rand(1000, 9999); - // try { - // if ($method === 'code') { - // \Mail::to($data['email'])->send(new \App\Mail\VerifyEmailCodeMail($code)); - // } else { - // $verificationUrl = url("api/auth/verify-email-link/{$token}?package={$packageNameValue}"); - // \Mail::to($data['email'])->send(new \App\Mail\VerifyEmailLinkMail($verificationUrl)); - // } - // } catch (\Exception $e) { - // return response()->json([ - // 'error' => $e->getMessage(), - // 'message' => 'لطفا آدرس ایمیل خود را بررسی کنید و مجدد تلاش کنید' - // ], 400); - // } + try { + if ($method === 'code') { + \Mail::to($data['email'])->send(new \App\Mail\VerifyEmailCodeMail($code)); + } else { + $verificationUrl = url("api/auth/verify-email-link/{$token}?package={$packageNameValue}"); + \Mail::to($data['email'])->send(new \App\Mail\VerifyEmailLinkMail($verificationUrl)); + } + } catch (\Exception $e) { + return response()->json([ + 'error' => $e->getMessage(), + 'message' => 'لطفا آدرس ایمیل خود را بررسی کنید و مجدد تلاش کنید' + ], 400); + } // try { // // Try sending email // \Mail::to($data['email'])->send(new \App\Mail\VerifyEmailCodeMail($code)); @@ -453,7 +453,71 @@ public function checkOTP(Request $request) 'token' => $token, ]); } +public function requestMobileVerification(Request $request) +{ + $data = $request->validate([ + 'mobile' => ['required', 'string', new MobileNumber], + ]); + $user = auth()->user(); + $mobile = MobileNumberHelper::formatMobile($data['mobile']); + + // چک اینکه موبایل مال یوزر دیگه نباشه + if (User::where('mobile', $mobile) + ->where('id', '!=', $user->id) + ->exists()) { + return response()->json([ + 'message' => 'mobile already taken' + ], 400); + } + + // حذف otp های قبلی + $user->otpTokens()->delete(); + + $otp = rand(10000, 99999); + + $user->otpTokens()->create([ + 'token' => $otp + ]); + + SendOTPJob::dispatch($mobile, (string)$otp); + + return response()->json([ + 'message' => 'otp sent successfully' + ]); +} +public function confirmMobileVerification(Request $request) +{ + $data = $request->validate([ + 'mobile' => ['required', 'string', new MobileNumber], + 'token' => 'required|string' + ]); + + $user = auth()->user(); + $mobile = MobileNumberHelper::formatMobile($data['mobile']); + + $otp = $user->otpTokens() + ->where('token', $data['token']) + ->where('created_at', '>', now()->subMinutes(5)) + ->first(); + + if (!$otp) { + return response()->json([ + 'message' => 'invalid or expired token' + ], 400); + } + + $otp->delete(); + + $user->update([ + 'mobile' => $mobile, + 'mobile_verified_at' => now(), + ]); + + return response()->json([ + 'message' => 'mobile verified successfully' + ]); +} public function updateProfile(Request $request) { $data = $request->validate([