Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rappasoft committed Nov 22, 2021
1 parent ab5356e commit 201e512
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ title: Known Issues
weight: 2
---

Curable:
Fixed:

- [This cache store is not supported. - torann/geoip](https://github.com/Torann/laravel-geoip/issues/147#issuecomment-528414630)
- When the session renews Laravel fires the Login event which results in a new login row [1](https://github.com/rappasoft/laravel-authentication-log/issues/13) [2](https://rappasoft.com/docs/laravel-authentication-log/v1/start/configuration#user-content-example-event-override)

Unsolved:

- [When the session renews Laravel fires the Login event which results in a new login row](https://github.com/rappasoft/laravel-authentication-log/issues/13)
- None
93 changes: 93 additions & 0 deletions docs/start/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ return [

// The database connection where the authentication_log table resides. Leave empty to use the default
'db_connection' => null,

// The events the package listens for to log (as of v1.3)
'events' => [
'login' => \Illuminate\Auth\Events\Login::class,
'failed' => \Illuminate\Auth\Events\Failed::class,
'logout' => \Illuminate\Auth\Events\Logout::class,
'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
],

'notifications' => [
'new-device' => [
Expand Down Expand Up @@ -84,3 +92,88 @@ class User extends Authenticatable
```

The package will listen for Laravel's Login, Logout, Failed, and OtherDeviceLogout events.

## Overriding default Laravel events

If you would like to listen to your own events you may override them in the package config (as of v1.3).

### Example event override

You may notice that Laravel [fires a Login event when the session renews](https://github.com/laravel/framework/blob/master/src/Illuminate/Auth/SessionGuard.php#L149) if the user clicked 'remember me' when logging in. This will produce empty login rows each time which is not what we want. The way around this is to fire your own `Login` event instead of listening for Laravels.

You can create a Login event that takes the user:

```php
<?php

namespace App\Domains\Auth\Events;

use Illuminate\Queue\SerializesModels;

class Login
{
use SerializesModels;

public $user;

public function __construct($user)
{
$this->user = $user;
}
}
```

Then override it in the package config:

```php
// The events the package listens for to log
'events' => [
'login' => \App\Domains\Auth\Events\Login::class,
...
],
```

Then call it where you login your user:

```php
event(new Login($user));
```

Now the package will only register actual login events, and not session re-authentications.

### Overriding in Fortify

If you are working with Fortify and would like to register your own Login event, you can append a class to the authentication stack:

In FortifyServiceProvider:

```php
Fortify::authenticateThrough(function () {
return array_filter([
...
FireLoginEvent::class,
]);
});
```

`FireLoginEvent` is just a class that fires the event:

```php
<?php

namespace App\Domains\Auth\Actions;

use App\Domains\Auth\Events\Login;

class FireLoginEvent
{
public function handle($request, $next)
{
if ($request->user()) {
event(new Login($request->user()));
}

return $next($request);
}
}
```

0 comments on commit 201e512

Please sign in to comment.