mercredi 1 avril 2020

Send verification e-mail, when it is changed by user later in laravel

I am developing a system with users, which must have their e-mail addresses verified. In registration that is OK by almost default laravel function (with used vue auth). However, I can give users an opinion, to change their e-mail address any time.

So I have stored users in my DB, and they have "email" column and "email_verified_at" column. When user is registrated, the verification e-mail is sent, and "email_verified_at" is default set to NULL. This is OK.

When user change his e-mail inside my app, as logged user, I have two problems:

1 How can I set NULL value again to the "email_verified_at"? It is datetime, so when user has his email verified, the date and time is stored there. When he change the e-mail address, I need to set this value again to NULL, how can I do that?

2 How can I force Laravel to send new verification e-mail with verification link? Is here any method or something like this?

I even tried using boot method in my user module, but it's not working either (not giving at least an error)

protected static function boot()
    {
        parent::boot();

        static::updating(function (User $setting) {
            if (in_array('email', $setting->getChanges())) {
                $setting->email_verified_at = null;
                $setting->sendEmailVerificationNotification();
            }
        });
    }

And following is my update function in the profile controller,

 public function update(Request $request, User $setting)
        {

            $changedAttributes = array_diff($request->all(), $setting->getAttributes());

            $validationRules = array_intersect_key([
               'name'      => ['required', 'alpha','min:2', 'max:255'],
               'last_name' => ['required', 'alpha','min:5', 'max:255'],
               'mobile'    => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
               2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
               4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
               'email'     => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
               'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
           ], $changedAttributes);

           if($request->hasFile('propic'))
           {
                $this->validate($request, [
                    'name' => ['required', 'alpha','min:2', 'max:255'],
                    'last_name' => ['required', 'alpha','min:5', 'max:255'],
                    'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|3[70]|7|1)\d{1,14}$/'],
                    'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
                    'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
                ],$request->all());

                $imageName = time().'.'.$request->propic->extension();  
                $request->propic->move(public_path('propics'), $imageName);
                $setting->propic=$imageName;
                $setting->name=$request->input('name');
                $setting->last_name=$request->input('last_name');
                $setting->mobile=$request->input('mobile');
                $setting->email=$request->input('email');
                $setting->update();
                return Redirect::back()->with('success',__('sentence.User updated successfully'));  
           }

           $this->validate($request, $validationRules);

           $setting->update($changedAttributes);

           return Redirect::back()->with('success',__('sentence.User updated successfully'));  

        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire