Laravel Authentication- Developing User Authentication System
Hello guys, here is another tutorial from W3Laravel. In this tutorial we are going to learn to authenticate an user to our web app. We often need to authenticate an user before he/she can access our some or all web pages. In this way we can easily identify which user is performing the actions. For example- While searching for a product in a E-commerce website we don't need to be authenticated but if we need to purchase an item we need to be authenticated by logging in to the website using your username and password.
Laravel Authentication- User Authentication System
Setting up
Setting up an user authentication system is quite easy for laravel users. We can say that we are not developing this, but we are just learning how we can user laravel's default authentication system.The very first thing we need to do is run an artisan command. It is preferred to run this command on a fresh installation of laravel.
php artisan make:auth
This command will create the files which we have to use for authenticating an user. For example- it will create all the routes for login and registering to the website. The required controllers that will handle the login and register requests and also a default login and register page using the bootstap framework.
Creating Database and tables
I assume that you how to create a MySQL database and configure it your web app. If not you can prefer reading the below article.How to create and configure database in Laravel
We even don't need to create tables and model manually for our app. The laravel authentication has already been created two migration files for users and password_resets table and also the User model. Just run this command-
php artisan migrate
This will create two tables to the database which you have configured to this app with all required columns.
Registering and Logging in to the App
To register for a new account to your web app visit http://your-app/register, fill the required fields and click on the register button. It will create a new account.
Registered user can visit- http://your-app/login and enter their credential to login to the app.
Protecting Routes from unauthenticated users
To restrict unauthenticated users to access our routes, we can use route middleware. Laravel provides auth middleware for restricting unauthenticated users. To protect a route use the following syntax for defining a route-Route::get('feed', function(){ //Can only be accessed by authenticated users echo "Welcome back"; })->middleware('auth');
This route can only be accessed by authenticated users, otherwise will be redirected to login page.
If you are using a controller then you can call middleware function to the constructor. So that any request to that controller can't be made without authentication.
public function __construct() { $this->middleware('auth'); }
Retrieving Authenticated User
The laravel authentication makes it very easy to retrieve the details of an authenticated users to our view. We can print the name and email of an authenticated user like this.<h1>Welcome {{Auth::user()->name}}</h1> <p>Your Email- {{Auth::user()->email}}</p>
Determining if the current user is authenticated
Using Laravel authentication we can easily find whether the current user is a guest or an authenticated user. This can be helpful if you want to print different messages for guests and authenticated users.@if(Auth::check()) Welcome {{Auth::user()->name}} @else Please Register @endif
Conclusion
Using Laravel Authentication we can easily implement the authentication system for our web app. It means now our user can register and log in to access the private section of our web app. If you find any trouble regarding following the tutorial Laravel Authentication- Developing User Authentication System, comment below. We are always there for you.
And one more thing, if you like this tutorial please share and help others needy too.
0 comments:
Post a Comment