Skip to content

Commit

Permalink
feat: redirect after login to entrance url
Browse files Browse the repository at this point in the history
  • Loading branch information
mshannaq committed Aug 20, 2023
1 parent 78cac54 commit e922392
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ class Auth extends BaseConfig
*/
public function loginRedirect(): string
{
$url = setting('Auth.redirects')['login'];
$session = session();
$url = $session->getTempdata('beforeLoginUrl') ?? setting('Auth.redirects')['login'];

return $this->getUrl($url);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Filters/SessionAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function before(RequestInterface $request, $arguments = null)
->with('error', $authenticator->getPendingMessage());
}

if (! url_is('login')) {
$session = session();
$session->setTempdata('beforeLoginUrl', current_url(), 300);
}

return redirect()->route('login');
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Authentication/Filters/SessionFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ public function testBlocksInactiveUsers(): void

setting('Auth.actions', ['register' => null]);
}

public function testStoreRedirectsToEntraceUrlIntoSession(): void
{
$result = $this->call('get', 'protected-route');
$result->assertRedirectTo('/login');
$this->assertNotEmpty($_SESSION['beforeLoginUrl']);
$this->assertSame(site_url('protected-route'), $_SESSION['beforeLoginUrl']);
}
}

0 comments on commit e922392

Please sign in to comment.