montrealinthefall's avatar

Password reset email not inserting proper URL

in my reset email being sent the url is being created without the APP_URL being inserted into it, and being sent in the reset email like this (just totally missing the URL of my website):

http://password/reset/{token}?email={email}

It's also using http and not https, as my app_url denotes (my side loads properly with https as well).

I have my app_url set in my env file as such:

APP_URL=https://mywebsiteurl.com

and my password reset link is created with this code:

$this->registerPolicies(); ResetPassword::createUrlUsing(function ($user, string $token) { return env('APP_URL').'/password/reset/'.$token.'?email='.$user->email;

But

Does anyone see a mistake that I've made that could cause this? Or is there any other information needed to debug?

Thank you

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

This code ResetPassword::createUrlUsing(function ($user, string $token) { return env('APP_URL').'/password/reset/'.$token.'?email='.$user->email; should be added in your AppServiceProvider not in your AuthServiceProvider.. and also make sure you use config('app.url') instead of env('APP_URL').

Just in case run php artisan config:clear in case your config is cached too.

1 like
montrealinthefall's avatar

@Nakov Thank you so much, this worked perfectly, mostly. When I initially tried it I used the code you suggested and I also moved it to AppServiceProvider like you said, and this gave me an error, shown below. So I moved it back to AuthServiceProvider just to test, but kept the code change you said, and it worked here. I'm sure the error is nothing surprising for someone that is experienced and knows the ins and outs of Laravel, but I'm still having to research every little thing.

When I initially started with Laravel I was brand new to PHP, so I started with this "kit" so to speak, called Wave, and it had a pre-built configuration for Laravel, Voyager, and some other things that helped me get started. so I'm assuming the way this is configured is why this works properly in AuthServiceProvider, but I wanted to ask you if you think this could pose some kind of problem? Or if it works, I'm assuming that's al that matters?

Either way, thank you for your help.

Aforementioned error:

Error

Nakov's avatar

@montrealinthefall AuthServiceProvider is used for the authorization and authentication modules of Laravel.. so this $this->registerPolicies(); should remain in the AuthServiceProvider class..

So just the code I copied above, ie the createUrlUsing on the ResetPassword component should be moved to the AppServiceProvider.

Snapey's avatar

Your initial mistake was using env() helper outside of your config files

Once you cache config, the env helper will not be initialised and will return null for all values

Please or to participate in this conversation.