Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#78 - Customize mail - Backend #83

Merged
merged 14 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Models;

use App\Notifications\SendResetPasswordEmail;
use App\Notifications\ResetPasswordNotification;
use App\Notifications\SendVerificationEmail;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\CanResetPassword;
Expand All @@ -16,6 +16,7 @@
use Spatie\Permission\Traits\HasRoles;

/**
* @param string $token
* @property int $id
* @property string $name
* @property string $surname
Expand Down Expand Up @@ -57,7 +58,7 @@ public function sendEmailVerificationNotification(): void

public function sendPasswordResetNotification($token): void
{
$this->notify(new SendResetPasswordEmail($token));
$this->notify(new ResetPasswordNotification($token));
}

protected function casts(): array
Expand Down
38 changes: 38 additions & 0 deletions app/Notifications/ResetPasswordNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ResetPasswordNotification extends Notification
{
use Queueable;

public function __construct(
protected string $token,
) {}

/**
* @return array<int, string>
*/
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved
public function via(object $notifiable): array
{
return ["mail"];
}

public function toMail(object $notifiable): MailMessage
{
$resetUrl = url("/auth/password/reset/" . $this->token . "&email=" . urlencode($notifiable->email));

return (new MailMessage())
->subject("Resetowanie hasła")
->view("emails.auth.reset-password", [
"user" => $notifiable,
"url" => $resetUrl,
]);
}
}
14 changes: 0 additions & 14 deletions app/Notifications/SendResetPasswordEmail.php

This file was deleted.

10 changes: 10 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace App\Providers;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -16,5 +18,13 @@ public function register(): void
public function boot(): void
{
JsonResource::withoutWrapping();
VerifyEmail::toMailUsing(function (object $notifiable, string $url) {
AleksandraKozubal marked this conversation as resolved.
Show resolved Hide resolved
return (new MailMessage())
->subject("Weryfikacja Adresu E-mail")
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved
->view("emails.auth.verify", [
"user" => $notifiable,
"url" => $url,
]);
});
}
}
28 changes: 28 additions & 0 deletions resources/views/emails/auth/reset-password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zresetuj hasło</title>
</head>

<header>
<img src="favicon.png" alt="{{ config('app.name') }} Logo" width="150" height="50">
<h1>{{ config('app.name') }}</h1>
</header>

<body>
<h1>Witaj {{ $user->name }},</h1>
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved

<p>Otrzymaliśmy prośbę o zresetowanie hasła dla Twojego konta.</p>

<p>Aby zresetować swoje hasło, kliknij poniższy link:</p>

<a href="{{ $url }}">Zresetuj hasło</a>

<p>Jeśli to nie Ty wysłałeś/aś tę prośbę, po prostu zignoruj tę wiadomość.</p>
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved

<p>Pozdrawiamy,<br>{{ config('app.name') }}</p>
</body>
</html>
27 changes: 27 additions & 0 deletions resources/views/emails/auth/verify.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Witaj w {{ config('app.name') }}</title>
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved
</head>

<header>
<img src="" alt="{{ config('app.name') }} Logo" width="150" height="50">
<h1>{{ config('app.name') }}</h1>
</header>

<body>
<p>Witaj {{ $user->name }},</p>
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved

<p>Aby zweryfikować swoje konto, kliknij poniższy link:</p>

<a href="{{ $url }}">Zweryfikuj E-mail </a>
PrabuckiDominik marked this conversation as resolved.
Show resolved Hide resolved

<p>Jeśli to nie Ty wysłałeś/aś tę prośbę, po prostu zignoruj tę wiadomość.</p>

<p>Pozdrawiamy,<br>{{ config('app.name') }}</p>
</body>

</html>
1 change: 0 additions & 1 deletion resources/views/emails/verify-email.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Feature/ForgotPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\Feature;

use App\Models\User;
use App\Notifications\SendResetPasswordEmail;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use JsonException;
Expand All @@ -30,7 +30,7 @@ public function testUserCanSendForgotPasswordRequest(): void

Notification::assertSentTo(
[$user],
SendResetPasswordEmail::class,
ResetPasswordNotification::class,
);
}

Expand Down
Loading