Search code examples
phplaravelemaillaravel-5email-verification

Laravel email verification link issue


In my laravel application's app url is something like this, admin.site and I'm registering users to my application from the admin panel.

And my client portal url is customer.site.

Once the admin creates an user from admin panel (admin.site) customer receive an account verification email. But the issue is now I need this verification link to be

customer.site/email/...

but the current link is like this

admin.site/email/...

So how can I change this verification link to customer.site

Following is my store function for customer controller

public function store(Request $request)
    {
        request()->validate([
            'name' => ['required', 'alpha','min:2', 'max:255'],
            'last_name' => ['required', 'alpha','min:2', 'max:255'],
            'email' => ['required','email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:12', 'confirmed','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/'],
            'mobile'=>['required', 'regex:/^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{8})$/','numeric','min:9'],
            'username'=>['required', 'string', 'min:4', 'max:10', 'unique:users'],   
            'roles'=>['required'],
            'user_roles'=>['required'],
        ]);

        //Customer::create($request->all());

        $input = $request->all();
        $input['password'] = Hash::make($input['password']);

        $user = User::create($input);
        $user->assignRole($request->input('roles'));

        event(new Registered($user));

        return redirect()->route('customers.index')
                        ->with('success','Customer created successfully. Verification email has been sent to user email.  ');
    }

I'm sending my verification email

event(new Registered($user));

As the customers have no access to the admin site it gives me 403 error message.


Solution

  • For an application that might send a lot of emails it is usefull to use notifcations for emails. An notifaction can be created by executing the following command:

    php artisan make:notification SendRegisterEmailNotifcation
    

    This command wil create a SendRegisterEmailNotifcation file that can be found by navigating to the app/Notifications/SendRegisterEmailNotifcation.php path. When you have done that and have customized the message, action and other possible things your store function would look like this. I've removed the validation and put it in a request. If you're intrested in how it works a example can found below.

    More information on notifcations can be found here: https://www.cloudways.com/blog/laravel-notification-system-on-slack-and-email/

    // CustomerController
    public function store(StoreCustomerRequest $request)
    {
        // Get input from the request and hash the password
        $input = $request->all();
        $input['password'] = Hash::make($input['password']);
    
        // Create user and assign role
        $user = User::create($input);
        $user->assignRole($request->input('roles'));
    
        // Send Register Email
        $user->notify(new SendRegisterEmailNotifcation);
    
         return redirect()->route('customers.index')
                            ->with('success','Customer created successfully. Verification email has been sent to user email.  ');
    }
    

    I would recommend creating Requests for validating the data. That way the controller will stay cleaner and you actually validate the data where laravel intended it to. //StoreCustomerRequest

    class StoreCustomerRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return Auth::check();
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            // @todo Add validation rules
            return [
                'name' => 'required|string|max:255',
                'last_name' => 'required|alpha|min:2|max:255'
                'email' => 'required|string|email|max:255|unique:users',
            ];
        }
    }
    

    Add the notifiable to your Customer Model. This has to be done to be able to send a notificaiton.

    // Customer model
    class Customer {
        use Notifiable;
    }