-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added mobile app token login email
- Loading branch information
1 parent
71b6075
commit 9c37273
Showing
11 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Mail\User; | ||
|
||
use App\Models\User; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
/** | ||
* Mailable class for notifying users about a new device login. | ||
* | ||
* This class constructs and sends an email to the user when a new login | ||
* to their account is detected from a mobile device, providing security | ||
* awareness and prompting them to review their account activity. | ||
*/ | ||
class DeviceAuthenticationLogIn extends Mailable implements ShouldQueue | ||
{ | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
/** | ||
* The user instance. | ||
*/ | ||
private readonly User $user | ||
) {} | ||
|
||
/** | ||
* Get the message envelope. | ||
*/ | ||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: __('Security Alert: New Device Login Detected'), | ||
); | ||
} | ||
|
||
/** | ||
* Get the message content definition. | ||
*/ | ||
public function content(): Content | ||
{ | ||
return new Content( | ||
markdown: 'mail.user.device-authentication-log-in', | ||
with: [ | ||
'user' => $this->user, | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken; | ||
|
||
/** | ||
* Represents a personal access token. | ||
* | ||
* This model extends Sanctum's personal access token and allows for checking | ||
* if the token is a mobile token. | ||
*/ | ||
class PersonalAccessToken extends SanctumPersonalAccessToken | ||
{ | ||
protected $table = 'personal_access_tokens'; | ||
|
||
/** | ||
* Determine whether the token is a mobile token or not. | ||
*/ | ||
public function isMobileToken(): bool | ||
{ | ||
return (bool) $this->getAttribute('mobile_at'); | ||
} | ||
|
||
/** | ||
* Get the casts array for the model's attributes. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
protected function casts(): array | ||
{ | ||
return [ | ||
'mobile_at' => 'bool', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
database/migrations/2024_08_12_095812_add_mobile_at_to_personal_access_tokens_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('personal_access_tokens', function (Blueprint $table) { | ||
$table->dateTime('mobile_at')->nullable(); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
resources/views/mail/user/device-authentication-log-in.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<x-mail::message> | ||
# Device Login | ||
|
||
Hey {{ $user->first_name }}, | ||
|
||
We noticed a new login to your account from a mobile device. | ||
|
||
If this was you, no further action is required. However, if you do not recognize this activity, we strongly recommend that you review your account settings and update your password immediately. | ||
|
||
To manage your API tokens and review any recent activity, please click the button below: | ||
|
||
<x-mail::button :url="route('profile.api')"> | ||
Review API Tokens | ||
</x-mail::button> | ||
|
||
If you have any questions or need assistance, please do not hesitate to contact our support team. | ||
|
||
Best regards, | ||
The {{ config('app.name') }} Team | ||
</x-mail::message> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\User; | ||
|
||
it('returns true if the token is a mobile token', function (): void { | ||
$user = User::factory()->create(); | ||
|
||
$user->createMobileToken('My Mobile Token'); | ||
|
||
$token = $user->tokens()->first(); | ||
|
||
$this->assertTrue($token->isMobileToken()); | ||
}); | ||
|
||
it('returns false if the token is not a mobile token', function (): void { | ||
$user = User::factory()->create(); | ||
|
||
$user->createToken('My Regular Token'); | ||
|
||
$token = $user->tokens()->first(); | ||
|
||
$this->assertFalse($token->isMobileToken()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters