Using Laravel 5's Authentication Facade

Authentication is part of nearly all the web applications you work with. It is extremely tedious to keep duplicating all the boilerplate code in every endeavor. Well, the great news is Laravel 5 rids you of this indifference by giving a ready-to-use authentication facade. 

I'm really going to presume you're starting off with a new Laravel 5 setup, but you can bypass some of these measures in case you have already done them. To begin with, you're going to set some environment variables in the .env file in the origin of your job. Essentially, these have to do with the database settings. !

This really is all you have to do to configure the database connection. But how does Laravel make use of these variants? Let us analyze the config/database.php file. You may see using the env() function. For instance, env('DB_HOST', 'localhost'). Laravel 5 uses this function to capture variants from the $_ENV and $_SERVER international arrays, which are automatically populated with the variables you define in the .env file. !

Run php artisan migrate:install --env=local in your terminal in the origin of your job to set up the migrations locally. Also see that there are two migrations already defined in the database/migrations folder. Using all these migrations, Laravel 5 creates a users along with a password_resets table, letting the default authentication boilerplate to operate. I'm really going to make a third migration to change the users table simply to demonstrate the best way to customize the default authentication set up.

As you are able to see, you've removed the name field and added two more fields for first_name and last_name with a maximum length of 50 characters. You've additionally added the code that rolls back these changes in the database. !

The validator function validates the information passed in from the user registration form. You've removed the default name field and added the first_name and last_name fields with a minimal span of three characters as well as a maximum length of 50 characters for both. The create function adds the documented user to the users table in the database, which means you just need to contain the first_name and last_name fields to it. !

The $fillable array defines which areas of the model are open to adjustment. You'd normally not contain fields which are auto-created into this array or areas which don't need a user's input signal such as the hash for a recall me keepsake. All you've done is upgrade the $fillable array to permit the first_name and last_name to be mass assignable.

To secure your courses and permit only logged-in users in order to get them, you should use the real middleware that is supplied by Laravel. The real middleware are available at programHttpMiddlewareAuthenticate.php. 

The password reset attribute sends the password reset link to the user's e-mail, so be sure to possess the mail setup set up in your Laravel job. The view template for the password reset e-mail is at resources/views/e-mails/password.blade.php. You may also configure several other fundamental choices in the config/auth.php file.



Posted on May 18, 2015 at 09:00 PM