From 97106ee6757f33ca754b2b1c70b3935cd975c392 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Mon, 5 Apr 2021 19:13:01 +0430 Subject: [PATCH] some bug fix and send email when user register or forget password --- app/Enums/tables.php | 5 ++++ app/Http/Controllers/AuthController.php | 35 ++++++++++++++++++------- app/Listeners/NotifHandler.php | 12 +++++---- app/Notifications/MailNotification.php | 2 +- resources/lang/fa/notification.php | 9 +++++++ 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/Enums/tables.php b/app/Enums/tables.php index 273bf4c..29465e8 100644 --- a/app/Enums/tables.php +++ b/app/Enums/tables.php @@ -63,6 +63,11 @@ return [ 'name' => 'Works', 'singular_name' => 'Work', ], + 'users' => [ + 'id' => 100, + 'name' => 'Users', + 'singular_name' => 'Users', + ], //Relation Table's 'business_user' => [ diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 1051350..f4cbdaa 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -5,6 +5,8 @@ namespace App\Http\Controllers; use App\Models\User; use App\Models\Business; use App\Models\Fingerprint; +use App\Notifications\MailNotification; +use Illuminate\Support\Facades\Notification; use Illuminate\Support\Str; use Illuminate\Http\Request; use Illuminate\Validation\Rule; @@ -15,6 +17,7 @@ use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Cache; use Laravel\Socialite\Facades\Socialite; use Illuminate\Session\TokenMismatchException; +use phpDocumentor\Reflection\Type; use Symfony\Component\HttpFoundation\Response; class AuthController extends Controller @@ -94,7 +97,7 @@ class AuthController extends Controller $request->merge(['password' => Hash::make($request->password)]); - $code_data = ['verification_code' => $this->sendVerificationCode()]; + $code_data = ['verification_code' => $this->sendVerificationCode(\request('email'), 'register')]; $method_data = ['method' => 'registerMain']; Cache::put($request->email, $request->all() + $code_data + $method_data, 3600); // remain one hour @@ -114,11 +117,16 @@ class AuthController extends Controller return $this->createFingerPrint(); } - public function sendVerificationCode($contact_way = null) + public function sendVerificationCode($contact_way, $type) { - $verification_code = 1234; // rand(10001, 99999) + $verification_code = rand(10001, 99999); - //send code for user with contact way + Notification::route('mail', $contact_way)->notify( new MailNotification([ + 'greeting' => __('notification.auth.verification.greeting'), + 'subject' => __('notification.auth.verification.subject'), + 'body' => __('notification.auth.verification.body', ['code' => $verification_code]), + 'link' => __('notification.auth.verification.link', ['email' => $contact_way, 'type' => $type]), + ])); return $verification_code; } @@ -136,11 +144,18 @@ class AuthController extends Controller 'verification_code' => 'required|string|min:4|max:4|in:'.$user_info['verification_code'] ]); - Cache::forget($request->email); +// Cache::forget($request->email); + + if (isset($user_info['method'])) { + Cache::forget($request->email); + return call_user_func('self::'.$user_info['method'], $user_info); + } - return isset($user_info['method']) ? - call_user_func('self::'.$user_info['method'], $user_info) : - \response()->json(['message' => 'Code verified successfully.'], Response::HTTP_OK,); + return \response()->json(['message' => 'Code verified successfully.'], Response::HTTP_OK,); + +// return isset($user_info['method']) ? +// call_user_func('self::'.$user_info['method'], $user_info) : +// \response()->json(['message' => 'Code verified successfully.'], Response::HTTP_OK,); } public function forgetPassword(Request $request) @@ -149,7 +164,7 @@ class AuthController extends Controller 'email' => 'required|email|exists:users,email' ]); - $code_data = ['verification_code' => $this->sendVerificationCode()]; + $code_data = ['verification_code' => $this->sendVerificationCode(\request('email', 'forget'))]; Cache::put($request->email, $request->all() + $code_data, 3600); // remain one hour @@ -178,6 +193,8 @@ class AuthController extends Controller Auth::setUser($user); + Cache::forget($request->email); + return $this->createFingerPrint(); } diff --git a/app/Listeners/NotifHandler.php b/app/Listeners/NotifHandler.php index c53def9..f6534c9 100644 --- a/app/Listeners/NotifHandler.php +++ b/app/Listeners/NotifHandler.php @@ -33,10 +33,12 @@ class NotifHandler if (class_exists($event_class)) { $event_class::dispatch($message); } - Notification::send(auth()->user(), new SocketNotification( - [ - 'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'), - 'payload'=>$business_info - ])); + if (auth()->user()) { + Notification::send(auth()->user(), new SocketNotification( + [ + 'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'), + 'payload'=> request('_business_info') ?? null + ])); + } } } diff --git a/app/Notifications/MailNotification.php b/app/Notifications/MailNotification.php index 8736089..914b2ed 100644 --- a/app/Notifications/MailNotification.php +++ b/app/Notifications/MailNotification.php @@ -73,6 +73,6 @@ class MailNotification extends Notification implements ShouldQueue ->greeting($this->message['greeting']) ->line($this->message['body']) ->subject($this->message['subject']) - ->action('Notification Action', url('/')); + ->action('بیشتر', $this->message['link'] ?? url('/')); } } diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 39d08d2..64213ab 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -85,6 +85,15 @@ return [ 'suspended' => 'حساب مسدود شد.', ], + 'auth' => [ + 'verification' => [ + 'greeting' => 'سلام کاربر گرامی!', + 'subject' => 'کد تایید', + 'body' => 'کد تایید شما :code', + 'link' => 'http://localhost:3000/verification?email=:email&type=:type' + ] + ], + 'sms' => [ 'templates' => [ 'template_name' => [