Search code examples
laraveleventslistener

How to send user's credentials in email


In my EventServiceProvider i have an event and listener like so

 protected $listen = [
        'App\Events\UserCreated' => [
            'App\Listeners\SendCredentials'
        ],
    ];

When a user is created by an admin their password is generated automatically so in order for them to access their account i have to send them their credentials via email

class UserCreated
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $user;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

}
class SendCredentials
{
    /**
     * Handle the event.
     *
     * @param  \App\Events\UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        $user = $event->user;
        Mail::to($user->email)->send(new SendUserMail($user));
    }
}

And this is how i dispatch the UserCreated event

public function createUser(){
        $this->validate();

        $randomPass = Str::random(8);
        $userCreds = ['email' => $this->email, 'password' => $randomPass];

        try{
            $test = User::firstOrCreate([
                'name' => $this->name,
                'email' => $this->email,
                'password' => Hash::make($randomPass),
                'email_verified_at' => Carbon::now(),
                'user_type' => (int)$this->selectedUser,
                'assigned_to' => (int)$this->selectedAssigned,
                'doctor_type' => $this->selectTypeDoc,
            ]);

            // Mail::to($this->email)->send(new SendUserMail($userCreds));
            UserCreated::dispatch($test);

            $this->reset();
            $this->emitUp('refreshParent');
            $this->dispatchBrowserEvent('swal-insert');

        }catch(\Exception $e){
            $this->dispatchBrowserEvent('createUser-error');
        }

        $this->closeModal();

    }

How can i send the password unencrypted in the email data


Solution

  • when user is created and password is hashed you are unable to retrieve the password in readable way.

    What I would do in your situation, is change UserCreated as follows:

    class UserCreated
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;
    
        public $user;
    
        public $password;
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct($user, $password)
        {
            $this->user = $user;
            $this->password = $password;
        }
    
    }
    

    I would also consider using getters and setters UserCreated instead of making them public.

    and you createUser method

    public function createUser(){
            $this->validate();
    
            $randomPass = Str::random(8);
            $userCreds = ['email' => $this->email, 'password' => $randomPass];
    
            try{
                $test = User::firstOrCreate([
                    'name' => $this->name,
                    'email' => $this->email,
                    'password' => Hash::make($randomPass),
                    'email_verified_at' => Carbon::now(),
                    'user_type' => (int)$this->selectedUser,
                    'assigned_to' => (int)$this->selectedAssigned,
                    'doctor_type' => $this->selectTypeDoc,
                ]);
    
                // Mail::to($this->email)->send(new SendUserMail($userCreds));
                UserCreated::dispatch($test, $randomPass);
    
                $this->reset();
                $this->emitUp('refreshParent');
                $this->dispatchBrowserEvent('swal-insert');
    
            }catch(\Exception $e){
                $this->dispatchBrowserEvent('createUser-error');
            }
    
            $this->closeModal();
    
        }
    

    and SendCredentials

    class SendCredentials
    {
        /**
         * Handle the event.
         *
         * @param  \App\Events\UserCreated  $event
         * @return void
         */
        public function handle(UserCreated $event)
        {
            Mail::to($user->email)->send(new SendUserMail($event->user, $event->password));
        }
    }