Skip to content

Commit 9eb38df

Browse files
author
Evan Sims
authored
Add LoginAttempting event for customizing authorization parameters (#382)
1 parent 29fbb0a commit 9eb38df

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

docs/Events.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ You can learn more about working with the Laravel event system in the [Laravel d
4040

4141
During login with the `Auth0\Laravel\Http\Controller\Stateful\Login` controller, the following events may be raised:
4242

43-
| Event | Description |
44-
| ------------------------------ | ----------------------------------------------------------------------------------- |
45-
| `Illuminate\Auth\Events\Login` | Raised when a user is logging in. The model of the user is provided with the event. |
43+
| Event | Description |
44+
| ------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
45+
| `Illuminate\Auth\Events\Login` | Raised when a user is logging in. The model of the user is provided with the event. |
46+
| `Auth0\Laravel\Contract\Event\Stateful\LoginAttempting` | Raised before the login redirect is issued, allowing an opportunity to customize parameters. |
4647

4748
## Callback Controller Events
4849

4950
During callback with the `Auth0\Laravel\Http\Controller\Stateful\Callback` controller, the following events may be raised:
5051

51-
| Event | Description |
52-
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
53-
| `Illuminate\Auth\Events\Attempting` | Raised when a user is returned to the application after authenticating with Auth0. This is raised before verification of the authentication process begins. |
54-
| `Illuminate\Auth\Events\Failed` | Raised when authentication with Auth0 failed. The reason is provided with the event as an array. |
52+
| Event | Description |
53+
| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54+
| `Illuminate\Auth\Events\Attempting` | Raised when a user is returned to the application after authenticating with Auth0. This is raised before verification of the authentication process begins. |
55+
| `Illuminate\Auth\Events\Failed` | Raised when authentication with Auth0 failed. The reason is provided with the event as an array. |
5556
| `Auth0\Laravel\Event\Stateful\AuthenticationFailed` | Raised when authentication with Auth0 failed. This provides an opportunity to intercept the exception thrown by the middleware, by using the event's `setThrowException()` method to `false`. You can also customize the type of exception thrown using `setException()`. |
56-
| `Illuminate\Auth\Events\Illuminate\Auth\Events\Validated` | Raised when authentication was successful, but immediately before the user's session is established. |
57-
| `Auth0\Laravel\Event\Stateful\AuthenticationSucceeded` | Raised when authentication was successful. The model of the authenticated user is provided with the event. |
57+
| `Illuminate\Auth\Events\Illuminate\Auth\Events\Validated` | Raised when authentication was successful, but immediately before the user's session is established. |
58+
| `Auth0\Laravel\Event\Stateful\AuthenticationSucceeded` | Raised when authentication was successful. The model of the authenticated user is provided with the event. |
5859

5960
## Logout Controller Events
6061

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auth0\Laravel\Contract\Event\Stateful;
6+
7+
interface LoginAttempting
8+
{
9+
/**
10+
* @param array $parameters The login parameters array.
11+
*/
12+
public function __construct(
13+
array $parameters = [],
14+
);
15+
16+
/**
17+
* Returns the login parameters array.
18+
*/
19+
public function getParameters(): array;
20+
21+
/**
22+
* Replace the login parameters array.
23+
*
24+
* @param array $parameters
25+
*/
26+
public function setParameters(array $parameters): self;
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auth0\Laravel\Event\Stateful;
6+
7+
use Auth0\Laravel\Contract\Event\Stateful\LoginAttempting as LoginAttemptingContract;
8+
use Auth0\Laravel\Event\Auth0Event;
9+
10+
final class LoginAttempting extends Auth0Event implements LoginAttemptingContract
11+
{
12+
public function __construct(
13+
private array $parameters = [],
14+
) {
15+
}
16+
17+
public function getParameters(): array
18+
{
19+
return $this->parameters;
20+
}
21+
22+
public function setParameters(array $parameters): self
23+
{
24+
$this->parameters = $parameters;
25+
26+
return $this;
27+
}
28+
}

src/Http/Controller/Stateful/Login.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Auth0\Laravel\Contract\Auth\Guard as GuardContract;
99
use Auth0\Laravel\Contract\Entities\Credential;
1010
use Auth0\Laravel\Contract\Http\Controller\Stateful\Login as LoginContract;
11+
use Auth0\Laravel\Event\Stateful\LoginAttempting;
1112
use Auth0\Laravel\Http\Controller\ControllerAbstract;
1213
use Illuminate\Http\Request;
1314
use Symfony\Component\HttpFoundation\Response;
@@ -34,7 +35,12 @@ public function __invoke(
3435
return redirect()->intended(config('auth0.routes.home', '/'));
3536
}
3637

37-
$url = $this->getSdk()->login();
38+
$event = new LoginAttempting();
39+
event($event);
40+
41+
$url = $this->getSdk()->login(
42+
params: $event->getParameters(),
43+
);
3844

3945
return redirect()->away($url);
4046
}

0 commit comments

Comments
 (0)