Laravel Jetstream basic setup with email verification in Laravel 8
Laravel Jetstream is a beautifully designed application scaffolding for Laravel. Jetstream includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management. Laravel Jetstream replaces and improves upon the legacy authentication UI scaffolding available for previous versions of Laravel.
In this tutorial we will show you the steps to setup the basic Jetstream setup with email verification after registering.
Step - 1 : Create new laravel application
First we need to create laravel 8 application in our local system help of run the following command in terminal.
composer create-project --prefer-dist laravel/laravel blog
Step - 2 : install jetstream package using composer
composer require laravel/jetstream
Step - 3 : install dependencies
After installing Jetstream, you should install and build your NPM dependencies
php artisan migrate
Step - 4 : Run Databse migrations
php artisan migrate
Step - 5 : Check autogenerated files in your project directory
After installing jetstream you will have autogenerated files in resources/views directory.
Step - 6 : Enable email verification
uncomment the relevant entry in the features configuration item of the config/fortify.php configuration file
'features' => [
...
Features::emailVerification(),
...
],
Step - 7 : Email setup
Next you need to setup your email settings through which reistration mail willbe sent. You can setup this in your .env file. Below is gmail account setup example. To send email through gmail account you need to enable the two factor email verification and generate application name nd password.you can check the steps for two factor email verification here.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
Step - 8 : Implement MustVerifyEmail interface
Edit app\Models\User.php as below
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'profile_photo_url',
];
}
Now run the application to see the output.You will see the login, register link.
After register one email will be sent to your registered email account. and by clicking on the verification link you can verify your account and then can login.
After login on top right corner there will be dropdown to manage profile, manage account, team, etc.
Copyright 2023 HackTheStuff