Implementing Basic Authorization With Laravel
Tags: laravel, php, tutorial, beginners
Published: 2023-03-26 05:55:06

## Introduction

Hello! In this tutorial I will show you how to implement basic authorization using the laravel framework. 😃

---

## Requirements

For this tutorial you will need the following:

- PHP and composer installed on your system (The installation process varies depending on your OS)
- Basic knowledge of PHP
- PHP driver to connect to your database (This example uses PostgreSQL)

---

## Creating The Project

Creating the project is as easy as just running the following command:

```bash
composer create-project --prefer-dist laravel/laravel auth-example
```

This will create a project called "auth-example" in the directory you ran the above command in. Next we need to set up the database that will used to store the user's credentials. 

---

## Creating And Configuring The Database

This tutorial will be focusing on PostgreSQL, but feel free to change it to mysql etc. 
First we need to actually create the database used, to do that run the following commands:

```bash
psql postgres

# This will log you in to the postgresql database
# Next create the database
create database laravel_example;

# Next we need to create a role that can access the created database.
create role laravel with LOGIN PASSWORD 'password' SUPERUSER;
```

The above will create a new database called "laravel_example", and a new user called "laravel" with the password "password", obviously in a production environment you would want a much harder to guess password. 😉

Next you will need to change your laravel ".env" file to let it know about the database that was just created. Open up ".env" file and edit the following:

```bash
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_example
DB_USERNAME=laravel
DB_PASSWORD=password
```

Now we can continue on creating the project.

---

## Finishing Up The Project

Now we need to actually set up the UI for the project, this can be done by running the following commands:

```bash
yarn
composer require laravel/ui
php artisan ui react --auth
yarn
```

The above commands initialize the node packages and then implements the React UI (You can use other UI frameworks but React is probably the most popular). Once implemented we run yarn again to finish off the installation. You may get asked to replace the current views, if so type yes to all of them.

Next we need to run migrations for the database, this can be done via the following command:

```bash
php artisan migrate
```

Then we can start the development server with the following:

```bash
# Start Laravel
php artisan serve

# Start react UI
yarn dev
```

Now if you point your browser to "http://localhost:8000" you should see the following:

![Start Page](https://i.ibb.co/rvQPK0f/laravel-start.png)

If you click on Register you should see the following:

![Register Page](https://i.ibb.co/mSVNv73/laravel-register.png)

Feel free to try and register an account, on success you will be taken to the dashboard page:

![Dashboard](https://i.ibb.co/J2JzYXd/laravel-dashboard.png)

Feel free to try logging out and logging back in via the login page:

![Login Page](https://i.ibb.co/CnMsp1x/laravel-login.png)

Well done you have now implemented basic authorization with laravel 😎

---

## Conclusion

In this tutorial I have shown how to implement basic authorization using the Laravel framework. I hope you learned something from this, as I had a lot of fun implementing it. 😄

I'm a beginner when it comes to Laravel so if you have any feedback, please let me know in the comments.

You can find the source for this project on my Github page:
https://github.com/ethand91/laravel-auth-example

Happy Coding! 😄

---

Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee. 

[![“Buy Me A Coffee”](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/ethand9999)

If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the [following course](https://algolab.so/p/algorithms-and-data-structure-video-course?affcode=1413380_bzrepgch)