Skip to content

Commit e76dda4

Browse files
authored
feat: redirectTo (#46)
feat: redirectTo
1 parent d46e602 commit e76dda4

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

docs/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ This setting determines if the algorithm will be included to the query string of
101101

102102
By default, this is set to `false`.
103103

104+
### $redirectTo
105+
106+
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.
107+
108+
By default, this is set to `null`.
109+
104110
### $redirect
105111

106112
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.

src/Config/SignedUrl.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class SignedUrl extends BaseConfig
4444
*/
4545
public bool $includeAlgorithmKey = false;
4646

47+
/**
48+
* In Filter - redirect to the given URI path
49+
* with error on failure.
50+
*/
51+
public ?string $redirectTo = null;
52+
4753
/**
4854
* In Filter - redirect to the previous page
4955
* with error on failure.

src/Filters/SignedUrl.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function before(RequestInterface $request, $arguments = null)
4444
try {
4545
$signedUrl->verify($request);
4646
} catch (SignedUrlException $e) {
47+
if ($path = $signedUrl->shouldRedirectTo()) {
48+
return redirect()->to($path)->with('error', $e->getMessage());
49+
}
50+
4751
if ($signedUrl->shouldRedirect()) {
4852
return redirect()->back()->with('error', $e->getMessage());
4953
}

src/SignedUrl.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ public function verify(IncomingRequest $request): bool
175175
return true;
176176
}
177177

178+
/**
179+
* Return redirectTo config option.
180+
*/
181+
public function shouldRedirectTo(): ?string
182+
{
183+
return $this->config->redirectTo;
184+
}
185+
178186
/**
179187
* Check if redirect option is enabled.
180188
*/

tests/SignedUrlTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ public function testVerifyThrowExceptionForExpiredUrl(): void
297297
$signedUrl->verify($request);
298298
}
299299

300+
public function testShouldRedirectTo(): void
301+
{
302+
$config = new SignedUrlConfig();
303+
$signedUrl = new SignedUrl($config);
304+
$this->assertNull($signedUrl->shouldRedirectTo());
305+
}
306+
300307
public function testShouldRedirect(): void
301308
{
302309
$config = new SignedUrlConfig();

0 commit comments

Comments
 (0)