Search

Laravel 8 Jetstream login with email verification

post-title

Laravel provides variety of login method for different projects whether it is web application or API application. Laravel Jetstream is one of the basic authentication package. Jetstream provides all funcionality including login, registration, email verification 2fa, and API authentication using Laravel Sanctum. Laravel Jetstream use Tailwind CSS for designing.

In this article we will use Laravel Jetstream for authentication method. We will create new Laravel 8 project and install Laravel Jetstream. Also Laravel Jetstream is not compitible with existing project. So lets start step by step.

  • Step: 1 Create fresh Laravel project
  • Step: 2 Install Laravel Jetstream
  • Step: 3 Install NPM packages
  • Step: 4 Database configuration and Migration run
  • Step: 5 Publish Livewire Stack componant
  • Step: 6 Email environment setup
  • Step: 7 Change in User model

So let's get started:

Step: 1. Create fresh Laravel project

Open CMD or Terminal and create new Laravel application using composer command.

composer create-project laravel/laravel jetstream

Now change the working directory to project root

cd jetstream

Step: 2. Install Laravel Jetstream

In the second step, we will install Laravel Jetstream package with below composer command.

composer require laravel/jetstream

After the package installation complete, you need to run jetstream:install artisan command. You can install Jetstream with Livewire or inertia. We will install it using Livewire.

php artisan jetstream:install livewire

Step: 3. Install NPM packages

In this step, we will install NPM packages using below commands one by one.

npm install
npm run dev

Step: 4. Database configuration and Migration run

After the all packages installed, we need to setup database and migration.

First you need to configure database in .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=jetstream
DB_USERNAME=root
DB_PASSWORD=root

And run the below migrate command to generate tables in database.

php artisan migrate

Step: 5. Publish Livewire Stack componant

Run the command to publish Livewire Stack componant

php artisan vendor:publish --tag=jetstream-views

Step: 6. Email environment setup

In the .env file, you may want to setup email configuration as we will use email verification on user register. So setup the email credentials as below.

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendmail.com
MAIL_PORT=465
MAIL_USERNAME=mailaddress@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=mailaddress@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

We will put email verification, so remove comment from the config/fortify.php file.

'features' => [
    ...
    Features::emailVerification(),
],

Step: 7. Change in User model

In the last step, you need to change in User model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable, HasFactory;
    ...
}

That's it Now run the project with artisan command.

php artisan serve

Now in the web browser, open http://localhost:8000 and go to login url.