Skip to content

Commit

Permalink
fix: make SendLoginLink work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ion Babin committed Apr 28, 2024
1 parent 3f1175f commit f7c4a2f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function store(StoreRequest $request): RedirectResponse
if (! $this->user->whereEmail($request->validated()['email'])->exists()) {
$user = $this->user->create($request->validated());
$this->token->create(['email' => $user->email]);
auth()->login($user);

}

// also generate some sort of notification (later)
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/SendLoginLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(private readonly User $user)
*/
public function handle(RequestedLogin $event): void
{
$user = $this->user->whereEmail($event->token->email);
$user->notify(new UserRequestedLogin($user));
$user = $this->user->whereEmail($event->token->email)->firstOrFail();
$user->notify(new UserRequestedLogin($user, $event->token));
}
}
4 changes: 1 addition & 3 deletions app/Notifications/UserRegistered.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Account successfully created!')
->greeting("Hi {$this->user->nickname}, your account has been successfully created! Please use the link below to log in.")
// TODO: this should be encoded
->line(url('login.do', ['token' => $this->user->token]))
->greeting("Hi {$this->user->nickname}, your account has been successfully created! A link that you can use to log in will be sent to you shortly.")
->line('Thank you for using our application!');
}

Expand Down
5 changes: 3 additions & 2 deletions app/Notifications/UserRequestedLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Notifications;

use App\Models\LoginToken;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
Expand All @@ -14,7 +15,7 @@ class UserRequestedLogin extends Notification
/**
* Create a new notification instance.
*/
public function __construct(public User $user)
public function __construct(public User $user, public LoginToken $token)
{
//
}
Expand All @@ -37,7 +38,7 @@ public function toMail(object $notifiable): MailMessage
return (new MailMessage)
->subject('Login link!')
->greeting("Hi {$this->user->nickname}, please use the link below to log in.")
->line(route('login.do', ['token' => $this->user->token]))
->line(route('login.do', ['token' => $this->token->value]))
->line('Thank you for using our application!');
}

Expand Down

0 comments on commit f7c4a2f

Please sign in to comment.