Ketika sudah registrasi kita perlu verifikasi user, Untuk akses web apps di pastikan user yang emailnya aktif (Authorized).
Setup .env agar bisa kirim melalui gmail
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassoword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Tambahkan AuthServiceProvider.php
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;
public function boot()
{
$this->registerPolicies();
VerifyEmail::toMailUsing(function ($notifiable, $url){
$spaUrl = $url;
return(new MailMessage)
->subject('Verify Email Address')
->line('Click the button below to verify your email address.')
->action('Verify Email Address', $spaUrl);
});
}
Tambahkan controller dan buka controllernya
php artisan make:controller EmailVerificationController
<?php
namespace App\Http\Controllers\API;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
class EmailVerificationController extends Controller
{
public function sendVerificationEmail(Request $request)
{
if($request->user()->hasVerifiedEmail()){
return [
'message' => 'Already Verified'
];
}
$request->user()->sendEmailVerificationNotification();
return [
'status' => 'verification-link-sent',
];
}
public function verify(Request $request)
{
$userID = $request['id'];
$user = User::findOrFail($userID);
$date = date("Y-m-d g:i:s");
$user->email_verified_at = $date;
$user->save();
return 'Email has been verified';
}
}
Tambahkan model User MustVerifyEmail
use Illuminate\Contracts\Auth\MustVerifyEmail;
Jangan lupa tambahkan juga di class
class User extends Authenticatable implements MustVerifyEmail
Tambahkan di routing api.php
Route::post('email/verification-notification', [EmailVerificationController::class, '
Route::get('email/verify/{id}', [EmailVerificationController::class, 'verify'])->name('verification.verify');
Komentar