Skip to content

Commit

Permalink
Merge pull request #793 from mshannaq/beforeLoginUrl
Browse files Browse the repository at this point in the history
feat: redirect after login to entrance url
  • Loading branch information
kenjis authored Aug 22, 2023
2 parents f334a92 + 098ac9b commit 970c67e
Show file tree
Hide file tree
Showing 3 changed files with 18 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
11 changes: 11 additions & 0 deletions tests/Authentication/Filters/SessionFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ public function testBlocksInactiveUsers(): void

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

public function testStoreRedirectsToEntraceUrlIntoSession(): void
{
$result = $this->call('get', 'protected-route');

$result->assertRedirectTo('/login');

$session = session();
$this->assertNotEmpty($session->get('beforeLoginUrl'));
$this->assertSame(site_url('protected-route'), $session->get('beforeLoginUrl'));
}
}

0 comments on commit 970c67e

Please sign in to comment.