Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from spacenate/improvements
Browse files Browse the repository at this point in the history
Publishes config, uses `passless.notification` config, fixes queueable notification seralization error.
  • Loading branch information
DarkGhostHunter committed Apr 3, 2019
2 parents 8488fa6 + eb2a770 commit 7fb939b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
42 changes: 23 additions & 19 deletions src/LoginNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Bus\Queueable;
use Illuminate\Config\Repository as Config;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Routing\UrlGenerator as Url;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
Expand All @@ -22,18 +21,25 @@ class LoginNotification extends Notification implements ShouldQueue
protected $remember;

/**
* The Config Repository
* The app name
*
* @var Config
* @var string
*/
protected $config;
protected $app_name;

/**
* The URL Generator
* The link lifetime in minutes
*
* @var Url
* @var integer
*/
protected $url;
protected $lifetime;

/**
* The Passless login route name
*
* @var string
*/
protected $passless_route;

/**
* URL Path of authentication
Expand All @@ -55,14 +61,14 @@ class LoginNotification extends Notification implements ShouldQueue
* @param bool $remember
* @param string|null $intended
* @param Config $config
* @param Url $url
*/
public function __construct(bool $remember,?string $intended, Config $config, Url $url)
public function __construct(bool $remember,?string $intended, Config $config)
{
$this->remember = $remember;
$this->intended = $intended;
$this->config = $config;
$this->url = $url;
$this->app_name = $config->get('app.name');
$this->lifetime = $config->get('passless.lifetime');
$this->passless_route = $config->get('passless.login.name');
}

/**
Expand All @@ -84,13 +90,11 @@ public function via($notifiable)
*/
public function toMail(Authenticatable $notifiable)
{
$lifetime = $this->config->get('passless.lifetime');

return (new MailMessage)
->greeting('Login to ' . $this->config->get('app.name'))
->greeting("Login to {$this->app_name}")
->line('Click the button to login. No password required.')
->action('Login', $this->createLoginUrl($notifiable, $lifetime))
->line("This link only last for $lifetime minutes.")
->action('Login', $this->createLoginUrl($notifiable, $this->lifetime))
->line("This link only lasts for {$this->lifetime} minutes.")
->line('Thank you for using our application!');
}

Expand All @@ -116,8 +120,8 @@ public function toArray($notifiable)
*/
public function createLoginUrl($notifiable, int $lifetime)
{
return $this->path = $this->url->temporarySignedRoute(
$this->config->get('passless.login.name'),
return $this->path = app('url')->temporarySignedRoute(
$this->passless_route,
now()->addMinutes($lifetime),
array_filter([
'id' => $notifiable->getAuthIdentifier(),
Expand All @@ -126,4 +130,4 @@ public function createLoginUrl($notifiable, int $lifetime)
])
);
}
}
}
7 changes: 5 additions & 2 deletions src/PasslessGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ public function attempt(array $credentials = [], $remember = false)
*/
public function sendLoginNotification(Authenticatable $user, bool $remember, string $intended = null)
{
$notification = app()->make(LoginNotification::class, [
// Check first if the config has a notification class set.
$notification = app('config')->get('passless.notification') ?? LoginNotification::class;

$notification = app()->make($notification, [
'remember' => $remember,
'intended' => $intended
]);

app(Dispatcher::class)->send($user, $notification);
}

}
}
5 changes: 4 additions & 1 deletion src/PasslessServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public function boot()
{
$this->bootGuardDriver();
$this->bootRoute();
$this->publishes([
__DIR__ . '/../config/passless.php' => config_path('passless.php'),
]);
}

/**
Expand Down Expand Up @@ -60,4 +63,4 @@ protected function bootRoute()
{
$this->loadRoutesFrom(__DIR__.'/../routes/routes.php');
}
}
}
12 changes: 11 additions & 1 deletion tests/Unit/LoginNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public function testMailShouldQueue()
$this->assertInstanceOf(ShouldQueue::class, $this->notification);
$this->assertArrayHasKey('Illuminate\Bus\Queueable', class_uses($this->notification));
}
}

public function testQueuedNotificationCanBeSerialized()
{
try {
serialize($this->notification);
$this->assertTrue(true);
} catch (\Exception $e) {
$this->fail($e->getMessage());
}
}
}

0 comments on commit 7fb939b

Please sign in to comment.