Skip to main content

Laravel Auth Tutorial

 After creating your project with the composer in Laravel, mostly next step is to enable the authentication. It is very simple to enable the Laravel auth with PHP artisan command. The most simple commands to follow is as below

Adding SQLite Database to Laravel 

First of all, you have to add the database into the ".env" file. The simplest database that you could use is the SQLite database with Laravel. You can create a simple SQLite file in the database folder and add the absolute path in the ".env" file. 


 >> touch database\database.sqlite 

The above command is for creating the database file. You may use create the file manually if you are using windows. 


You can change the .env file like this. 



DB_CONNECTION=sqlite
DB_DATABASE=/Applications/MAMP/htdocs/rtest/database/database.sqlite


Enable Auth Middleware 

The Larvel Authentication documentation explains it very well. According to that documentation, you need to use the Laravel Auth middleware like follows

First, you may wish to enable the Laravel UI which handles all the routes for the auth. Here is what the documentation says about it 


Laravel's laravel/ui package provides a quick way to scaffold all of the routes and views you need for authentication using a few simple commands:


You can add this package with these commands 

composer require laravel/ui "^1.0" --dev

php artisan ui vue --auth

Next, simply apply the migrations with the PHP Artisan command.

php artisan migrate

New Laravel Application with Auth

If you want to enable this auth scaffolding with a new Laravel application you can use the following command.  


laravel new blog --auth

Authenticated User Detail

Once you are logged in, you may retrieve the details about the authenticated user detail with the following code. 


use Illuminate\Support\Facades\Auth;

$user = Auth::user(); //If you want to get the ID of the use
$id = Auth::id();
//If you want to get the ID of the user


Laravel Rollback Migrations

if in any case, you wish to undo your migrations you may use this simple command


 >> php artisan migrate:reset 

Comments

Popular posts from this blog

Create Your first Django Project via Python Virtualenvironment

 Python virtual environment is especially for Python 2 which support is now ended. In Python 3 the virtualenv is called the venv . Today we are going to create our first Django project in Python3 with the virtual environment. The virtual environment helps us separate different versions of libraries and manage different libraries for different projects. It also provides us clean installation for every project.  Let's create a new project for Django Python 3. Open a command window and navigate to your desired directory for creating your Django Python Projects. In my case, I always prefer the Django-Projects directory under the main Python-Projects  Directory in My Documents.  Create Python venv for Django Project Now let's create a virtual environment for Python 3 and the Django project which we are just going to create. If you go to the official venv documentation  you will find pretty good details about all the required steps to create it. We are just following ...