Dynamic domain in reset password link Laravel 8

391 Views Asked by At

I'm aware that password reset link can be customized by adding the below function in AuthServiceProvider.php

ResetPassword::createUrlUsing(function ($user, string $token) {
     return 'https://example.com/reset-password?token='.$token;
});

This is my sendResetPassword function

public function sendResetPassword(Request $request) {
        $request->validate(['email' => 'required|email']);

        $status = Password::sendResetLink(
            $request->only('email')
        );

        if ($status === Password::RESET_LINK_SENT) {
            return response()->json(['message' => __($status)], 200);
        } else {
            return response()->json(['message' => __($status)], 500);
        }
}

Now I'm wondering if there is a way to pass a domain from the sendResetPassword $request to the createUrlUsing function. The main purpose of this is to avoid hardcoding the frontend URL in my API. I just want that the forgot password form in my frontend sends the email and also the domain.

1

There are 1 best solutions below

0
Luciano On BEST ANSWER

Not sure if this is the best approach, but as soon I posted the question I found that this is a working solution:

ResetPassword::createUrlUsing(function ($user, string $token) {
     return $this->app->request->headers->get('origin').'/reset-password?token='.$token;
});