Edit File: AuthenticationService.php
<?php namespace App\Services\Auth; use App\Enums\LoginType; use App\Enums\OTPType; use App\Models\User; use App\Traits\GeneralTrait; use App\Traits\UploadTrait; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\DB; class AuthenticationService extends AuthBaseService { use GeneralTrait, UploadTrait; private $model; private $entity; public function __construct($model) { $this->model = $model; parent::__construct($model); } public function loginOrRegister($request) { // Start a database transaction DB::beginTransaction(); try { $user = $this->findOrCreateUser($request); // define the identifier type $identifier_type = $this->getIdentifierType($request, true); // Send verification code to the target entity $user->sendVerificationCode($identifier_type, OTPType::VERIFICATION); // If user is blocked, return blocked if ($user->is_blocked) { return $this->response('blocked', __('auth.blocked'), $user); } // successMsg $successMsg = $user->wasRecentlyCreated ? __('auth.registered_code_sent') : __('auth.logined_code_sent'); // Commit the transaction DB::commit(); // Return success response with user details return $this->response( 'success', $successMsg, $user, ); } catch (\Exception $e) { // Rollback the transaction in case of an error DB::rollback(); // Return error response return $this->response('fail', $e->getMessage(), []); } } /** * Registers a new user based on the provided data. * @param $request * @return array */ public function register($request): array { // Start a database transaction DB::beginTransaction(); try { // Create a new entity $entity = $this->model::create($request); $this->entity = $entity; // define the identifier type $identifier_type = $this->getIdentifierType($request, true); // Send verification code to the target entity $entity->sendVerificationCode($identifier_type, OTPType::VERIFICATION); // Commit the transaction DB::commit(); // Return success response with target entity details return [ 'key' => 'success', 'msg' => __('auth.registered'), 'user' => $entity ]; } catch (\Exception $e) { // Rollback the transaction in case of an error DB::rollback(); // Return error response return [ 'key' => 'fail', 'msg' => $e->getMessage(), 'user' => [] ]; } } /** * Complete the user profile based on the provided data. * @param $request * @return array */ public function completeProfile($request): array { try { DB::beginTransaction(); $this->entity = auth()->user(); $this->entity->update($request); DB::commit(); return [ 'key' => 'success', 'msg' => __('auth.profile_completed'), 'user' => $this->entity->refresh() ]; } catch (\Exception $e) { DB::rollback(); return [ 'key' => 'fail', 'msg' => $e->getMessage(), 'user' => [] ]; } } /** * Complete the user interests based on the provided data. * @param $request * @return array */ public function completeInterests($request): array { try { DB::beginTransaction(); $this->entity = auth()->user(); if (isset($request['interest_option_ids'])) { $this->entity->interestOptions()->sync($request['interest_option_ids']); } $this->entity->update([ 'bio' => $request['bio'] ?? '', ]); DB::commit(); return $this->response('success', __('auth.interests_added'), $this->entity->refresh()); } catch (\Exception $e) { DB::rollback(); return $this->response('fail', $e->getMessage(), []); } } /** * Resend the verification code to the user based on the provided phone and country code or email. * @param $request * @return array */ public function resendCode($request): array { // Find the entity $entity = $this->getEntity($request); // define the identifier type $identifier_type = $this->getIdentifierType($request); // Send the verification code to the user $entity->sendVerificationCode($identifier_type, OTPType::VERIFICATION); // Return the success message and the updated user data return $this->response('success', __('auth.code_re_send'), $entity->refresh()); } /** * Activate the user based on the provided data. * @param $request * @return array */ public function activate($request): array { // Find the entity $entity = $this->getEntity($request); // activate the account $entity->markAsActive(); // Return the response data return $this->response('success', __('auth.activated'), $entity->refresh()); } /** * Login the user based on the provided data. * @param $request * @return array */ public function login($request): array { // Find the entity $entity = $this->getEntity($request); // define the identifier type $identifier_type = $this->getIdentifierType($request); $response = $this->response('success', __('auth.signed'), $entity); // If entity does not exist, return failure if (!$entity) { $response = $this->response('fail', __('auth.incorrect_key_or_phone'), []); } elseif ((isset($request['way']) && $request['way'] == LoginType::NORMAL) && !Hash::check($request['password'], $entity->password)) { // If password is incorrect, return failure $response = $this->response('fail', __('auth.incorrect_pass'), []); } elseif ($entity->is_blocked) { // If entity is blocked, return blocked $response = $this->response('blocked', __('auth.blocked'), []); } elseif (!$entity->is_active) { // If entity is not active, return not active $entity->sendVerificationCode($identifier_type, OTPType::VERIFICATION); $response = $this->response('needActive', __('auth.not_active'), $entity); } elseif ($entity->needComplete()) { // If entity is not active, return not active $response = $this->response('waitingApprove', __('auth.not_approved'), $entity); } elseif (isset($request['way']) && $request['way'] == LoginType::CODE) { // If all checks pass, return verification code $entity->sendVerificationCode($identifier_type, OTPType::LOGIN); $response = $this->response('needVerification', __('auth.phone_code_sent'), $entity); } return $response; } /** * Verify the user's phone number to login. * @param $request * @return array */ public function verifyLogin($request): array { // Find the entity $entity = $this->getEntity($request); // define the identifier type $identifier_type = $this->getIdentifierType($request); // check if the user has authOtp $success = $entity->checkOtp($request->code, OTPType::LOGIN, $identifier_type); if ($success == 'fail') { return $this->response('fail', __('auth.code_invalid'), []); } elseif ($success == 'expired') { return $this->response('fail', __('auth.code_expired'), []); } // delete the user's otp $entity->otps()->where(['type' => OTPType::LOGIN, 'code' => $request->code])->delete(); // Return the response data return $this->response('success', __('auth.signed'), $entity->refresh()); } /** * Forget the user's password based on the provided data. * @param $request * @return array */ public function forgetPasswordSendCode($request): array { // Find the entity $entity = $this->getEntity($request); if (!$entity) { return $this->response('fail', __('auth.incorrect_key_or_phone'), []); } // define the identifier type $identifier_type = $this->getIdentifierType($request); // Send the verification code to the user $entity->sendVerificationCode($identifier_type, OTPType::FORGET_PASSWORD); return $this->response('success', __('apis.success'), $entity->refresh()); } /** * Check the user's password reset code based on the provided data. * @param $request * @return array */ public function forgetPasswordCheckCode($request): array { // Find the entity $entity = $this->getEntity($request); $response = $this->response('fail', __('auth.incorrect_key_or_phone'), []); if ($entity) { // define the identifier type $identifier_type = $this->getIdentifierType($request); // check if the user has authOtp $success = $entity->checkOtp($request->code, OTPType::FORGET_PASSWORD, $identifier_type); if ($success == 'fail') { $response['msg'] = __('auth.code_invalid'); } elseif ($success == 'expired') { $response['msg'] = __('auth.code_expired'); } else { $response = ['key' => 'success', 'msg' => __('auth.code_checked'), 'user' => $entity->refresh()]; } } return $response; } /** * Reset the user's password based on the provided data. * @param $request * @return array */ public function resetPassword($request): array { // Find the entity $entity = $this->getEntity($request); try { // Start a database transaction DB::beginTransaction(); // delete the user's otp $entity->otps()->where(['type' => OTPType::FORGET_PASSWORD, 'code' => $request->code])->delete(); // Update the user's password $entity->update(['password' => $request['password']]); // Commit the transaction DB::commit(); // Return success message return [ 'key' => 'success', 'msg' => __('auth.password_changed') ]; } catch (\Exception $e) { // Rollback the transaction in case of an error DB::rollback(); // Return failure message return [ 'key' => 'fail', 'msg' => $e->getMessage() ]; } } public function updateLocation($request) { auth()->user()->update($request); return [ 'key' => 'success', 'msg' => __('auth.location_updated'), 'user' => auth()->user() ]; } /** * Logout the user. * @return array */ public function logout(): array { // Find the entity auth()->user()->logout(); // Return success message return [ 'key' => 'success', 'msg' => __('apis.loggedOut') ]; } /** * Delete the user's account. * @return array */ public function deleteAccount(): array { // Find the entity $entity = auth()->user(); try { // Start a database transaction DB::beginTransaction(); // delete the user's tokens $entity->tokens()->delete(); // delete the user's account $entity->delete(); // Commit the transaction DB::commit(); // Return success message return [ 'key' => 'success', 'msg' => __('auth.account_deleted') ]; } catch (\Exception $e) { // Rollback the transaction in case of an error DB::rollback(); // Return failure message return [ 'key' => 'fail', 'msg' => $e->getMessage() ]; } } protected function findOrCreateUser($request) { // Attempt to find the user by phone number or email $user = User::when(isset($request['phone']), function ($query) use ($request) { $query->where('phone', $request['phone']) ->where('country_code', $request['country_code']); }) ->when(isset($request['email']) && !isset($request['phone']), function ($query) use ($request) { $query->where('email', $request['email']); }) ->first(); // If user doesn't exist, create a new one if (!$user) { $user = User::create($request); } return $user; } protected function response($key, $msg, $data = []) { return [ 'key' => $key, 'msg' => $msg, 'user' => $data ]; } }
Back to File Manager