Create Authentication in Laravel

Create Authentication in Laravel

Step 1: Install Laravel Ensure you have Laravel installed on your system. You can either install it globally or use Laravel Valet, Laravel Homestead, or Laravel Sail for local development. if you are new read more.

Step 2: Create a New Laravel Project Open your terminal or command prompt, navigate to the desired directory, and run the following command to create a new Laravel project:

composer create-project laravel/laravel myproject

create project in laravel

Replace myproject with the desired name of your project.

Step 3: Set Up the Database Configure your database credentials in the .env file located in the root directory of your Laravel project. Modify the following lines with your database details:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Save the changes after updating the values.

Step 4: Generate Authentication Laravel Breeze provides a convenient way to generate the authentication scaffolding. Run the following command in your terminal:

composer require laravel/breeze:1.9.2

Once you run the above bash command then run the below commands.

php artisan breeze:install
npm install
npm run dev

If you get an error for npm install Nodejs and then restart your machine and try again it will resolve your problem.

Next, you may navigate to your application /login or /register URLs in your web browser. All of Breeze’s routes are defined within the routes/auth.php file.

This command will generate the necessary controllers, views, and routes for user registration, login, and password reset.

Step 5: Migrate the Database Next, run the database migration command to create the required tables in your database:

php artisan migrate

This command will create the necessary tables, including users and password_resets.

Step 6: Start the Development Server You can now start the Laravel development server by running the following command:

php artisan serve

This will start the server at http://localhost:8000.

Home page

Login Page.

Step 7: Test the Authentication Open your web browser and visit http://localhost:8000 to see your Laravel application. You will find the registration and login links in the top-right corner of the page. Click on the “Register” link to create a new user account. After registering, you can use the “Login” link to authenticate and access protected areas of your application.

That’s it! You have successfully set up basic authentication in your Laravel application. From here, you can customize and enhance the authentication system according to your specific requirements by modifying the generated views, controllers, and routes.