diff --git a/app/Http/Controllers/Frontend/ProfileController.php b/app/Http/Controllers/Frontend/ProfileController.php index 9093924..377e155 100644 --- a/app/Http/Controllers/Frontend/ProfileController.php +++ b/app/Http/Controllers/Frontend/ProfileController.php @@ -9,7 +9,9 @@ use App\Models\User; use Illuminate\Support\Facades\Session; use Auth; use App\Mail\OtpMail; +use App\Models\UserOtpModel; use Mail; +use Carbon\Carbon; use App\Http\Controllers\Frontend\LoginController as sendOTP; class ProfileController extends Controller @@ -125,12 +127,172 @@ class ProfileController extends Controller 'address' => $request->address, 'profile_image' => $image, ]); + + if ($addUser && $user->email != $request->email && $user->contact_number != $request->contact_number) { + + //update + $validator = validator::make($request->all(), [ + 'email' => 'required|unique:users,email', + ], [ + 'required' => 'The :attribute field must be required', + 'unique' => 'The :attribute field must be unique', + ]); + $validationMessage = $this->validationError($validator); + if ($validationMessage) { + return response()->json(['status' => 400, 'message' => $validationMessage]); + } + $emailotp = rand(1000, 9999); + $mailData = [ + 'title' => 'Mail from Jerichoalternatives.in', + 'body' => 'This is for testing email using smtp.' + ]; + + Mail::to($request->email)->send(new OtpMail($mailData, $emailotp)); + + + //mobile no, + + $validator = validator::make($request->all(), [ + 'contact_number' => 'required|unique:users,contact_number,' . $request->user()->id . '', + ], [ + 'required' => 'The :attribute field must be required', + 'unique' => 'The :attribute field must be unique', + ]); + $otp = rand(1000, 9999); + // Session::put('contact_number', $request->newcontact_number); + // Session::put('mobile_otp', $otp); + $this->thirdPartyOTP($request->contact_number, $otp); + + $usersOTPS = UserOtpModel::updateOrCreate( + ['user_id' => $user->id], + [ + 'email_otp' => $emailotp, + 'contact_otp' => $otp, + 'expire_at' => Carbon::now()->addMinutes('5'), + ] + ); + return response()->json([ + 'status' => 200, + 'message' => 'Details Updated Successfully!', + 'email_otp' => $emailotp, + 'mobile_otp' => $otp + ]); + + } else if ($addUser && $user->email != $request->email) { + //update + $validator = validator::make($request->all(), [ + 'email' => 'required|unique:users,email', + ], [ + 'required' => 'The :attribute field must be required', + 'unique' => 'The :attribute field must be unique', + ]); + $validationMessage = $this->validationError($validator); + if ($validationMessage) { + return response()->json(['status' => 400, 'message' => $validationMessage]); + } + $emailotp = rand(1000, 9999); + $mailData = [ + 'title' => 'Mail from Jerichoalternatives.in', + 'body' => 'This is for testing email using smtp.' + ]; + + Mail::to($request->email)->send(new OtpMail($mailData, $emailotp)); + $usersOTPS = UserOtpModel::updateOrCreate( + ['user_id' => $user->id], + [ + 'email_otp' => $emailotp, + // 'contact_otp' => $otp, + 'expire_at' => Carbon::now()->addMinutes('5'), + ] + ); + + return response()->json(['status' => 200, 'message' => 'Details Updated Successfully!', 'email_otp' => $emailotp]); + + } else if ($addUser && $user->contact_number != $request->contact_number) { + //mobile no, + + $validator = validator::make($request->all(), [ + 'contact_number' => 'required|unique:users,contact_number,' . $request->user()->id . '', + ], [ + 'required' => 'The :attribute field must be required', + 'unique' => 'The :attribute field must be unique', + ]); + $otp = rand(1000, 9999); + // Session::put('contact_number', $request->newcontact_number); + // Session::put('mobile_otp', $otp); + $this->thirdPartyOTP($request->contact_number, $otp); + $usersOTPS = UserOtpModel::updateOrCreate( + ['user_id' => $user->id], + [ + // 'email_otp' => $emailotp, + 'contact_otp' => $otp, + 'expire_at' => Carbon::now()->addMinutes('5'), + ] + ); + return response()->json(['status' => 200, 'message' => 'Details Updated Successfully!', 'mobile_otp' => $otp]); + + } if ($addUser) { return response()->json(['status' => 200, 'message' => 'Details Updated Successfully!']); } return response()->json(['status' => 400, 'message' => 'Error Updating Details!'], 400); } + public function verifyOtpAPI(Request $request) + { + try { + + $isVerificationFor = $request->is_verify_for; //1=email, 2=mobile , + $userId = $request->user()->id; + + $userOtpData = UserOtpModel::where('user_id', $userId)->first(); + $userData = User::where('id', $userId)->first(); + + if (!$userOtpData || !$userData) { + return response()->json(['status' => 400, 'message' => 'User Data Not Found in database'], 400); + } + + + if ($isVerificationFor == 1) { + + $emailOtp = $request->email_otp; + $emailToUpdate = $request->email; + + $userOtpData = UserOtpModel::where('user_id', $userId)->where('email_otp',$emailOtp)->first(); + if (!$userOtpData) { + return response()->json(['status' => 400, 'message' => 'OTP Did Not Matched!'], 400); + } + if (now() > $userOtpData->expire_at) { + return response()->json(['status' => 400, 'message' => 'OTP has been expired!'], 400); + } + + $userData->email = $emailToUpdate; + $userData->save(); + } + if ($isVerificationFor == 2) { + $contactOtp = $request->contact_otp; + $contactToUpdate = $request->contact_no; + + $userOtpData = UserOtpModel::where('user_id', $userId)->where('contact_otp',$contactOtp)->first(); + if (!$userOtpData) { + return response()->json(['status' => 400, 'message' => 'OTP Did Not Matched!'], 400); + } + if (now() > $userOtpData->expire_at) { + return response()->json(['status' => 400, 'message' => 'OTP has been expired!'], 400); + } + + $userData->contact_number = $contactToUpdate; + $userData->save(); + + + } + return response()->json(['status' => 200, 'message' => 'Your OTP verified Successfully!']); + + } catch (\Exception $e) { + return response()->json(['status' => 400, 'message' => 'Error Updating Details!'], 400); + } + } + public function getUser() { try { @@ -170,7 +332,7 @@ class ProfileController extends Controller 'required' => 'The :attribute field must be required', 'unique' => 'The :attribute field must be unique', ]); - + $validationMessage = $this->validationError($validator); if ($validationMessage) { return response()->json(['status' => 400, 'message' => $validationMessage]); @@ -192,9 +354,9 @@ class ProfileController extends Controller public function sendEmailOTPApi(Request $request) { - + $validator = validator::make($request->all(), [ - 'newEmail' => 'required|unique:users,email,' , + 'newEmail' => 'required|unique:users,email', ], [ 'required' => 'The :attribute field must be required', 'unique' => 'The :attribute field must be unique', @@ -208,12 +370,13 @@ class ProfileController extends Controller 'title' => 'Mail from Jerichoalternatives.in', 'body' => 'This is for testing email using smtp.' ]; - Session::put('newEmail', $request->newEmail); - Session::put('otp', $otp); + // Session::put('newEmail', $request->newEmail); + // Session::put('otp', $otp); Mail::to($request->newEmail)->send(new OtpMail($mailData, $otp)); return response()->json([ 'status' => 200, 'message' => 'OTP has been sent to your email', + 'your_otp' => $otp ]); } @@ -221,7 +384,7 @@ class ProfileController extends Controller { // dd('hello'); if ($request->email_otp) { - if ((int)$request->email_otp == Session::get('otp')) { + if ((int) $request->email_otp == Session::get('otp')) { $updateUserProfile = User::where('id', Auth::guard('users')->user()->id)->update([ 'email' => Session::get('newEmail'), ]); @@ -256,16 +419,18 @@ class ProfileController extends Controller { $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_URL => 'https://restapi.smscountry.com/v0.1/Accounts/4F7T5SbGyV7HBrEHxmX4/SMSes/', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => 'POST', - CURLOPT_POSTFIELDS => '{ + curl_setopt_array( + $curl, + array( + CURLOPT_URL => 'https://restapi.smscountry.com/v0.1/Accounts/4F7T5SbGyV7HBrEHxmX4/SMSes/', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => '{ "Text": "Dear Investor, Your login OTP is ' . $otp . '. - Blue Feather Ventures Pvt Ltd.", "Number": "91' . $number . '", "SenderId": "BLUFVL", @@ -273,11 +438,12 @@ class ProfileController extends Controller "DRNotifyHttpMethod": "POST", "Tool": "API" }', - CURLOPT_HTTPHEADER => array( - 'Content-Type: application/json', - 'Authorization: Basic NEY3VDVTYkd5VjdIQnJFSHhtWDQ6emFXdFEyTlV3ZlZROHB6dGRvVlRZUFdibG01Y1AxRldsbWl2WlVrbg==' - ), - )); + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/json', + 'Authorization: Basic NEY3VDVTYkd5VjdIQnJFSHhtWDQ6emFXdFEyTlV3ZlZROHB6dGRvVlRZUFdibG01Y1AxRldsbWl2WlVrbg==' + ), + ) + ); $response = curl_exec($curl); @@ -290,7 +456,7 @@ class ProfileController extends Controller { // dd(Session::get('mobile_otp'), Session::get('contact_number'), $request->mobile_otp); if ($request->mobile_otp) { - if ((int)$request->mobile_otp == Session::get('mobile_otp')) { + if ((int) $request->mobile_otp == Session::get('mobile_otp')) { $updateUserProfile = User::where('id', Auth::guard('users')->user()->id)->update([ 'contact_number' => Session::get('contact_number'), ]); diff --git a/app/Models/UserOtpModel.php b/app/Models/UserOtpModel.php new file mode 100644 index 0000000..3b524ee --- /dev/null +++ b/app/Models/UserOtpModel.php @@ -0,0 +1,14 @@ + ['auth:sanctum']], function () { Route::post("update-profile", [ProfileController::class, 'updateAPI']); Route::post("send-email-otp", [ProfileController::class, 'sendEmailOTPApi']); + Route::post("verify-user-email-or-contact-otp", [ProfileController::class, 'verifyOtpAPI']); + + + Route::post('logout', [AuthController::class, 'userLogout']); // Route::post("update-profile", [ProfileController::class, 'updateAPI']);