Edit File: ProfileController.php
<?php namespace App\Http\Controllers\Api\User; use App\Http\Requests\Api\User\Profile\VerifyUpdatingPhoneRequest; use App\Models\UserUpdate; use App\Services\auth\ProfileBaseService; use Illuminate\Http\Request; use App\Traits\ResponseTrait; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Hash; use App\Http\Requests\Api\User\Profile\UpdateProfileRequest; use App\Http\Requests\Api\User\Profile\UpdatePasswordRequest; use App\Http\Requests\Api\User\Profile\ChangePhoneSendCodeRequest; use App\Http\Requests\Api\User\Profile\ChangePhoneCheckCodeRequest; use App\Http\Requests\Api\User\Profile\UpdateLangRequest; use App\Http\Requests\Api\User\Profile\UpdatePhoneSendCodeRequest; use Illuminate\Http\JsonResponse; class ProfileController extends Controller { use ResponseTrait; protected $service; public function __construct(ProfileBaseService $service) { $this->service = $service; } /** * Get the user profile * * @return JsonResponse */ public function getProfile(): JsonResponse { // Get the user profile $data = $this->service->getProfile(); return $this->response($data['key'], $data['msg'], $data['user']); } /** * Update the user profile * * @param UpdateProfileRequest $request * @return JsonResponse */ public function update(UpdateProfileRequest $request) { // Update the user profile $data = $this->service->updateProfile($request->validated()); // Return the success response with the message and user resource return $this->response('success', $data['msg'], $data['user']); } /** * Update the user's password * * @param UpdatePasswordRequest $request * @return JsonResponse */ public function updatePassword(UpdatePasswordRequest $request) { // Update the user's password in the database auth()->user()->update($request->validated()); // Return the success message return $this->successMsg(__('apis.updated')); } /** * Check if the provided password matches the user's password * * @param Request $request * @return JsonResponse */ public function checkPassword(Request $request): JsonResponse { // Check if the provided password matches the user's password if (!Hash::check($request->password, auth()->user()->password)) { // If the password is incorrect, return an error message return $this->failMsg(trans('auth.incorrect_pass')); } // If the password is correct, return a success message return $this->successMsg(__('auth.correct_password')); } /** * BEGAIN::Main Scenario for updating the phone number */ /** * Send a verification code to the user's old phone number * * @param ChangePhoneSendCodeRequest $request * @return JsonResponse */ public function changePhoneSendCode(ChangePhoneSendCodeRequest $request) { // Update or create a new user update record $update = UserUpdate::updateOrCreate([ 'user_id' => auth()->id(), 'type' => 'phone', ], [ 'country_code' => $request->country_code, 'phone' => $request->phone, 'code' => '1' ])->refresh(); // Send the code to the user's phone number auth()->user()->sendCodeAtSms($update->code, $update->full_phone); // Return a success message return $this->successMsg(__('apis.success')); } /** * Resend the verification code to the user's old phone number * * @param ChangePhoneSendCodeRequest $request * @return JsonResponse */ public function changePhoneReSendCode(ChangePhoneSendCodeRequest $request) { // Update or create a new user update record $update = UserUpdate::updateOrCreate([ 'user_id' => auth()->id(), 'type' => 'phone', 'country_code' => $request->country_code, 'phone' => $request->phone, ], [ 'code' => '1' ])->refresh(); // Send the code to the user's phone number auth()->user()->sendCodeAtSms($update->code, $update->full_phone); // Return a success message return $this->successMsg(__('apis.success')); } /** * Check if the provided code matches the user's old phone number * * @param ChangePhoneCheckCodeRequest $request * @return JsonResponse */ public function changePhoneCheckCode(ChangePhoneCheckCodeRequest $request) { // Find the user update record with the provided code $update = UserUpdate::where([ 'user_id' => auth()->id(), 'type' => 'phone', 'code' => $request->code ])->first(); // If no user update record is found, return an error message if (!$update) { return $this->failMsg(trans('auth.code_invalid')); } // Update the user's phone and country code with the values from the user update record auth()->user()->update([ 'phone' => $update->phone, 'country_code' => $update->country_code ]); // Delete the user update record $update->delete(); // Return a success message return $this->successMsg(__('auth.phone_updated')); } /** * END::Main Scenario for updating the phone number */ /** * BEGAIN::Second Scenario for updating the phone number */ /** * Send a verification code to the user's old phone number * @param UpdatePhoneSendCodeRequest $request * @return JsonResponse */ public function sendCodeToOldPhone(UpdatePhoneSendCodeRequest $request): JsonResponse { // Send a verification code to the user's old phone number $data = $this->service->sendCodeToOldPhone($request); // Return the response return $this->response($data['key'], $data['msg'], $data['user']); } /** * Verify the user's old phone number * @param VerifyUpdatingPhoneRequest $request * @return JsonResponse */ public function verifyOldPhone(VerifyUpdatingPhoneRequest $request): JsonResponse { // Verify the user's old phone number $data = $this->service->verifyOldPhone($request); // Return the response return $this->response($data['key'], $data['msg'], $data['user']); } /** * Send a verification code to the user's new phone number * @param UpdatePhoneSendCodeRequest $request * @return JsonResponse */ public function sendCodeToNewPhone(UpdatePhoneSendCodeRequest $request): JsonResponse { // Send a verification code to the user's new phone number $data = $this->service->sendCodeToNewPhone($request); // Return the response return $this->response($data['key'], $data['msg'], $data['data']); } /** * Verify the user's new phone number * @param VerifyUpdatingPhoneRequest $request * @return JsonResponse */ public function verifyNewPhone(VerifyUpdatingPhoneRequest $request): JsonResponse { // Verify the user's new phone number $data = $this->service->verifyNewPhone($request); // Return the response return $this->response($data['key'], $data['msg'], $data['user']); } /** * END::Second Scenario for updating the phone number */ /** * Update language * @param UpdateLangRequest $request * @return JsonResponse */ public function updateLanguage(UpdateLangRequest $request): JsonResponse { // Update the user's language $this->service->updateLanguage($request); // Return a success message return $this->successMsg(__('apis.updated')); } }
Back to File Manager