Skip to content

Commit

Permalink
feat: redirectTo (#46)
Browse files Browse the repository at this point in the history
feat: redirectTo
  • Loading branch information
michalsn committed Oct 8, 2023
1 parent d46e602 commit e76dda4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ This setting determines if the algorithm will be included to the query string of

By default, this is set to `false`.

### $redirectTo

This setting is used in the Filter to determine whether we will redirect user to the given URI path with the `error`, when URL will not be valid or expired.

By default, this is set to `null`.

### $redirect

This setting is used in the Filter to determine whether we will redirect user to the previous page with the `error`, when URL will not be valid or expired.
Expand Down
6 changes: 6 additions & 0 deletions src/Config/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class SignedUrl extends BaseConfig
*/
public bool $includeAlgorithmKey = false;

/**
* In Filter - redirect to the given URI path
* with error on failure.
*/
public ?string $redirectTo = null;

/**
* In Filter - redirect to the previous page
* with error on failure.
Expand Down
4 changes: 4 additions & 0 deletions src/Filters/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function before(RequestInterface $request, $arguments = null)
try {
$signedUrl->verify($request);
} catch (SignedUrlException $e) {
if ($path = $signedUrl->shouldRedirectTo()) {
return redirect()->to($path)->with('error', $e->getMessage());
}

if ($signedUrl->shouldRedirect()) {
return redirect()->back()->with('error', $e->getMessage());
}
Expand Down
8 changes: 8 additions & 0 deletions src/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ public function verify(IncomingRequest $request): bool
return true;
}

/**
* Return redirectTo config option.
*/
public function shouldRedirectTo(): ?string
{
return $this->config->redirectTo;
}

/**
* Check if redirect option is enabled.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/SignedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ public function testVerifyThrowExceptionForExpiredUrl(): void
$signedUrl->verify($request);
}

public function testShouldRedirectTo(): void
{
$config = new SignedUrlConfig();
$signedUrl = new SignedUrl($config);
$this->assertNull($signedUrl->shouldRedirectTo());
}

public function testShouldRedirect(): void
{
$config = new SignedUrlConfig();
Expand Down

0 comments on commit e76dda4

Please sign in to comment.