From 5d77e776426318557faaad68dddbc87e484a29f5 Mon Sep 17 00:00:00 2001 From: mohamamad Date: Thu, 26 Sep 2019 13:01:10 +0330 Subject: [PATCH] mobile verification --- .../MobileVerificationCodeGenerated.php | 28 +++++++++++++++++ app/Events/MobileVerified.php | 3 +- ...obileVerificationCodeGeneratedListener.php | 26 ++++++++++++++++ app/Providers/EventServiceProvider.php | 6 +++- app/Traits/MustVerifyMobileNumberTrait.php | 31 +++++++++++++++---- 5 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 app/Events/MobileVerificationCodeGenerated.php create mode 100644 app/Listeners/MobileVerificationCodeGeneratedListener.php diff --git a/app/Events/MobileVerificationCodeGenerated.php b/app/Events/MobileVerificationCodeGenerated.php new file mode 100644 index 0000000..aa433f9 --- /dev/null +++ b/app/Events/MobileVerificationCodeGenerated.php @@ -0,0 +1,28 @@ +user = $user; + $this->generationTime = $generationTime; + } + +} diff --git a/app/Events/MobileVerified.php b/app/Events/MobileVerified.php index 64f7b93..fcc4c46 100644 --- a/app/Events/MobileVerified.php +++ b/app/Events/MobileVerified.php @@ -3,11 +3,12 @@ namespace App\Events; use App\Classes\Verification\MustVerifyMobileNumber; +use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; class MobileVerified { - use SerializesModels; + use Dispatchable, SerializesModels; /** * The verified user. diff --git a/app/Listeners/MobileVerificationCodeGeneratedListener.php b/app/Listeners/MobileVerificationCodeGeneratedListener.php new file mode 100644 index 0000000..38031d1 --- /dev/null +++ b/app/Listeners/MobileVerificationCodeGeneratedListener.php @@ -0,0 +1,26 @@ +generationTime; + $user = $event->user; +// Carbon::createFromTimestamp($timeStamp); + Log::info('MobileVerificationCodeGeneratedListener : '.$user->id.' - '.$timeStamp); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 0f0259c..4cc9028 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,8 +3,10 @@ namespace App\Providers; use App\Events\CountOption; +use App\Events\MobileVerificationCodeGenerated; use App\Events\MobileVerified; use App\Listeners\CountOptionListener; +use App\Listeners\MobileVerificationCodeGeneratedListener; use App\Listeners\MobileVerifiedListener; use App\Listeners\SendMobileVerificationNotification; use Illuminate\Auth\Events\Registered; @@ -28,8 +30,10 @@ class EventServiceProvider extends ServiceProvider ], CountOption::class => [ CountOptionListener::class + ], + MobileVerificationCodeGenerated::class => [ + MobileVerificationCodeGeneratedListener::class ] - ]; /** diff --git a/app/Traits/MustVerifyMobileNumberTrait.php b/app/Traits/MustVerifyMobileNumberTrait.php index 52f40cf..10714e2 100644 --- a/app/Traits/MustVerifyMobileNumberTrait.php +++ b/app/Traits/MustVerifyMobileNumberTrait.php @@ -8,9 +8,11 @@ namespace App\Traits; +use App\Events\MobileVerificationCodeGenerated; use App\Notifications\MobileVerified; use App\Notifications\VerifyMobile; -use Illuminate\Support\Facades\Cache; +use Carbon\Carbon; +use Illuminate\Support\Facades\Log; trait MustVerifyMobileNumberTrait { @@ -21,7 +23,7 @@ trait MustVerifyMobileNumberTrait */ public function hasVerifiedMobile() { - return $this->mobile_verified_at !== null; + return ! is_null($this->mobile_verified_at); } /** @@ -44,10 +46,10 @@ public function markMobileAsVerified() public function sendMobileVerificationNotification() { if ($this->setMobileVerificationCode()) { - $this->notify(new VerifyMobile()); +// $this->notify(new VerifyMobile()); } } - + /** * generate a verification code for given user * @@ -56,11 +58,28 @@ public function sendMobileVerificationNotification() */ public function setMobileVerificationCode(): bool { - $verificationCode = random_int(10000, 99999); + Log::info('before event'); + event(new MobileVerificationCodeGenerated($this , Carbon::now('Asia/Tehran'))); + return true; - return $this->forceFill([ + + $verificationCode = $this->getMobileVerificationCode(); + if(isset($verificationCode)) { + return true; + } + + + $verificationCode = random_int(10000, 99999); + $result = $this->forceFill([ 'mobile_verified_code' => $verificationCode, ])->save(); + + if($result){ + event(new MobileVerificationCodeGenerated($this , Carbon::now('Asia/Tehran'))); + + return true; + } + return false; } /**