Creating Laravel Application with Jetstream Authentication

In this tutorial, we are going to create a Laravel application with Jetstream Authentication. So first of all Let’s understand what is Laravel Jetstream.

Jetstream should only be installed into new Laravel applications. Attempting to install Jetstream into an existing Laravel application will result in unexpected behavior and issues.

What is Laravel Jetstream?

A Laravel Jetstream is a beautifully designed application starter kit for Laravel and provides the perfect starting point for your next Laravel application. Jetstream provides the implementation for your application’s login, registration, email verification, two-factor authentication, session management, API via Laravel Sanctum, and optional team management features.

Laravel Jetstream offers us two available front-end stacks.

  1. Livewire
  2. Inertia

Both of them provide us with productive and powerful functionality and design. However, you can choose any stack you wish. Let us give you a brief about both before creating our application.

Livewire

Livewire is a library that uses a blade template to provide a user interface and functionality to our application. When using Livewire, you may pick and choose which portions of your application will be a Livewire component, while the remainder of your application can be rendered as the traditional Blade templates you are used to.

Inertia

The Inertia stack provided by Jetstream uses Vue.js as its template language. With Inertia.js, you get to build fully client-side rendered apps (Vue.js, React, Svelte), but build those apps in a more classic server-side “monolith” way. With Inertia, you don’t need an API. Just create controllers and views, with plain old server-side routing, and Inertia turns it into a single-page application.

Create a new Laravel Application

Let’s begin to create our Laravel application with Jetstream authentication. First of all, create a new project.

You can create a fresh Laravel application with one of the below commands:

composer create-project --prefer-dist laravel/laravel Application_name

Change Project Name with Application_name.

Database Configuration

Let’s configure the database for our application. If you don’t have a database then create a new one. After creating the database open the .env file from the root directory of your project. if the .env file is missing from the project then copy content from the .env.example and create a file. .env file defines many common environment variables

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=**********
DB_USERNAME=*********
DB_PASSWORD=*********

Installing Jetstream to Laravel Application

We are going to use composer to install Jetstream into our project so open the terminal or command prompt and navigate to your project root directory. Enter the below command to install Jetstream:

composer require laravel/jetstream

Installing Jetstream Livewire/Inertia

After installing the Jetstream package, we have to execute the jetstream:install Artisan command. We have to pass the stack name that we want to use in the application (livewire or inertia). In addition, we can also pass –teams switch to enable team support.

Enter the Following command to the terminal:

//For Livewire stack
php artisan jetstream:install livewire
//If you want to install livewire with teams
php artisan jetstream:install livewire --teams

//For Inertia
php artisan jetstream:install inertia
//For Inertia with teams
php artisan jetstream:install inertia --teams

Finalizing The Installation

After installing Jetstream, we have to install and build our NPM dependencies. For that enter the below commands to the terminal or command prompt:

npm install
npm run dev

It will take some time to install NPM dependencies. After installation, you will receive messages like Laravel Mix, and build successfully.

Migrate Database

Let’s run all outstanding migrations, and execute the migrate Artisan command:

php artisan migrate

Our Laravel application with Jetstream Authentication is created successfully. For testing application, we need to run the local server using the artisan command:

php artisan serve
//If you want to run application on specific port then use this command.
php artisan serve –port=8080

Open the below URL in the browser to see the output:

http://127.0.0.1:8000

Conclusion

Here, we have created a new Laravel application using Jetstream authentication. It’s upon you to choose Livewire or Inertia. But be aware that installing Jetstream after developing some functionality can cause you errors.