hmawla

Untitled

Nov 28th, 2021
657
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * @param Request $request
  3.      * @return JsonResponse|null
  4.      * @throws ValidationException
  5.      */
  6.     public function requestSms(Request $request): ?JsonResponse
  7.     {
  8.         $data = $this->validate($request, [
  9.             'phone' => 'required|string',
  10.             'device' => 'required|string'
  11.         ]);
  12.  
  13.         if ($this->hasTooManyLoginAttempts($request)) {
  14.             $this->fireLockoutEvent($request);
  15.  
  16.             $this->sendLockoutResponse($request);
  17.         }
  18.  
  19.  
  20.  
  21.         /**
  22.          * @var PhoneVerification $phoneVerification
  23.          */
  24.         $phoneVerification = PhoneVerification::query()
  25.             ->where('valid_until', '>', now())
  26.             ->where('phone', $data['phone'])
  27.             ->first();
  28.  
  29.         if($phoneVerification == null){
  30.             $phoneVerification = PhoneVerification::query()
  31.                 ->create([
  32.                     'phone' => $data['phone'],
  33.                     'verification_code' => mt_rand(1000, 9999) . '',
  34.                     'last_request_ip' => AuthUtils::getRealIpAddr(),
  35.                     'last_request_device' => $data['device'],
  36.                     'last_request_at' => now(),
  37.                     'valid_until' => now()->addDay()
  38.                 ]);
  39.         }
  40.  
  41.         if ($phoneVerification->next_request_at == null || $phoneVerification->next_request_at->isBefore(now())) {
  42.             //can send SMS
  43.             if ($phoneVerification->request_count == 0) {
  44.                 $phoneVerification->next_request_at = now()->addMinutes(5);
  45.             }
  46.             else if ($phoneVerification->request_count == 1) {
  47.                 $phoneVerification->next_request_at = now()->addMinutes(30);
  48.             } else if ($phoneVerification->request_count == 2) {
  49.                 $phoneVerification->next_request_at = now()->addHour();
  50.             } else if ($phoneVerification->request_count == 3) {
  51.                 $phoneVerification->next_request_at = now()->addHours(6);
  52.             } else if ($phoneVerification->request_count == 4) {
  53.                 $phoneVerification->next_request_at = now()->addDay();
  54.             }
  55.  
  56.             $phoneVerification->request_count++;
  57.             $phoneVerification->last_request_ip = AuthUtils::getRealIpAddr();
  58.             $phoneVerification->last_request_device = $data['device'];
  59.             $phoneVerification->last_request_at = now();
  60.             $phoneVerification->save();
  61.  
  62.             /**
  63.              * @var User $user
  64.              */
  65.             $user = User::query()
  66.                 ->where('phone', $phoneVerification->phone)
  67.                 ->first();
  68.  
  69.             if(!($user != null && $user->role_id == Constants::ROLE_POS_USER)){
  70.                 SmsService::sendSms($data['phone'], 'Your Class App verification code is ' . $phoneVerification->verification_code);
  71.             }
  72.             return response()->json($phoneVerification);
  73.         }
  74.         $this->incrementLoginAttempts($request);
  75.         abort(response([
  76.             'message' => 'You cannot request another SMS yet!',
  77.             'phone_verification' => $phoneVerification
  78.         ], 400));
  79.         return null;
  80.     }
  81.  
  82.     /**
  83.      * @param Request $request
  84.      * @return JsonResponse
  85.      * @throws ValidationException
  86.      */
  87.     public function login(Request $request): JsonResponse
  88.     {
  89.  
  90.         $data = $this->validate($request, [
  91.             'verificationId' => 'required',
  92.             'verificationCode' => 'required|string'
  93.         ]);
  94.  
  95.         if ($this->hasTooManyLoginAttempts($request)) {
  96.             $this->fireLockoutEvent($request);
  97.  
  98.             $this->sendLockoutResponse($request);
  99.         }
  100.  
  101.         /**
  102.          * @var PhoneVerification $phoneVerification
  103.          */
  104.         $phoneVerification = PhoneVerification::query()
  105.             ->where('id', $data['verificationId'])
  106.             ->first();
  107.  
  108.         if($phoneVerification == null){
  109.             $this->incrementLoginAttempts($request);
  110.             abort(response(['message' => 'Invalid verification ID!'], 400));
  111.         }
  112.  
  113.         if($phoneVerification->verification_code != $data['verificationCode']){
  114.             $this->incrementLoginAttempts($request);
  115.             abort(response(['message' => 'Invalid verification code!'], 422));
  116.         }
  117.  
  118.         if($phoneVerification->valid_until->isBefore(now())){
  119.             $this->incrementLoginAttempts($request);
  120.             abort(response(['message' => 'Verification code expired!'], 422));
  121.         }
  122.  
  123.         $phoneVerification->valid_until = now();
  124.         $phoneVerification->save();
  125.         /**
  126.          * @var User $user
  127.          */
  128.         $user = User::query()->where('phone', '=', $phoneVerification->phone)->first();
  129.         if ($user == null)
  130.             $user = User::query()->create([
  131.                 'phone' => $phoneVerification->phone,
  132.                 'role_id' => Constants::ROLE_REGULAR_USER,
  133.             ]);
  134.         else {
  135.             if ($user->deleted_at != null)
  136.                 abort(response(['message' => 'Account invalid, please contact our support or try again later.'], 401));
  137.             if ($user->is_locked)
  138.                 abort(response(['message' => 'Account locked, please contact our support or try again later.'], 401));
  139.         }
  140.         /**
  141.          * @var User $user
  142.          */
  143.         $accessToken = $user->createToken(env('APP_NAME'))->plainTextToken;
  144.         $token = [
  145.             'access_token' => $accessToken
  146.         ];
  147.         Log::info("Authenticated login request for user #$user->id : $user->name");
  148.         return \response()->json($token);
  149.     }
RAW Paste Data