Skip to content

Commit

Permalink
add custom url callback (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-banciulea authored Jul 19, 2021
1 parent 77971b3 commit 7a858c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Services/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function register(Request $request)
return RegisterService::make($request, $this);
}

public function forgotPassword(Request $request)
public function forgotPassword(Request $request, string $url = null)
{
return ForgotPasswordService::make($request);
return ForgotPasswordService::make($request, $url);
}

/*
Expand Down
6 changes: 3 additions & 3 deletions src/Services/ForgotPasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class ForgotPasswordService
{
public static function make(Request $request)
public static function make(Request $request, string $url = null)
{
ResetPassword::createUrlUsing(function ($notifiable, $token) {
$withToken = str_replace(['{token}'], $token, config('restify.auth.password_reset_url'));
ResetPassword::createUrlUsing(function ($notifiable, $token) use ($url) {
$withToken = str_replace(['{token}'], $token, $url ?? config('restify.auth.password_reset_url'));
$withEmail = str_replace(['{email}'], $notifiable->getEmailForPasswordReset(), $withToken);

return url($withEmail);
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/Authentication/AuthServiceForgotPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ public function test_email_was_sent_and_contain_token()
});
}

public function test_email_was_sent_and_has_default_or_custom_url_callback()
{
Notification::fake();

$user = $this->register();
$request = new Request([], []);
$request->merge(['email' => $user->email]);

$this->authService->forgotPassword($request);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$this->assertEquals(
"https://laravel-restify.dev/password/reset?token={$notification->token}&email={$user->email}",
call_user_func($notification::$createUrlCallback, $user, $notification->token),
);

return true;
});

$this->authService->forgotPassword(
$request,
'https://subdomain.domain.test/password/reset?token={token}&email={email}',
);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$this->assertEquals(
"https://subdomain.domain.test/password/reset?token={$notification->token}&email={$user->email}",
call_user_func($notification::$createUrlCallback, $user, $notification->token),
);

return true;
});
}

public function test_reset_password_invalid_payload()
{
$this->expectException(ValidationException::class);
Expand Down

0 comments on commit 7a858c5

Please sign in to comment.