Today we will learn how to create a login and registration form and we will see that Laravel creates it automatically with only one command. We will also see how to customize the form by adding a new field and also verify that only active users can login.
Create authentication system
Laravel is able to generate everything you need for the authentication of a project (forms, classes, routes, etc.) simply with a command and from there we can customize everything we want. So, we save a lot of time, since it is a task that in almost all the projects we would have to do.
1 |
php artisan make:auth |
We will see that many files have been created, including some views. One of them is a layout and what I’m going to do is to extend the views we created earlier and extend them from a layout, change it for this one. The change would be this:
1 |
@extends('layouts.app') |
Now it has everything more or less the same style, although they would need to touch up some things, since I’m not using Bootstrap classes then it looks very close to the margins. But by looking at the other views, we can adapt it easily.
Modify register form
To add a field to the registration form, first of all we have to create a migration so that the field is added to the database table. I, for example, have decided to put the country field, but you can put anything.
1 |
php artisan make:migration add_country_field_to_users_table |
Open the file and put this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddCountryFieldToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function(Blueprint $table) { $table->string('country'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function(Blueprint $table) { $table->dropColumn('country'); }); } } |
Execute the migrations:
1 |
php artisan migrate |
Now we have to add the field to the form ( resources/views/auth/register.blade.php ), so add this code aqfter the email section:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div class="form-group{{ $errors->has('country') ? ' has-error' : '' }}"> <label for="country" class="col-md-4 control-label">Country</label> <div class="col-md-6"> <input id="country" type="text" class="form-control" name="country" value="{{ old('country') }}" required> @if ($errors->has('country')) <span class="help-block"> <strong>{{ $errors->first('country') }}</strong> </span> @endif </div> </div> |
Once this is done, we have to add it to the method that check it and save it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'country' => 'required|string', 'password' => 'required|string|min:6|confirmed', ]); } // protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'country' => $data['country'], 'password' => bcrypt($data['password']), ]); } |
Finally, we have to indicate in the model that it will fill the country field:
1 2 3 |
protected $fillable = [ 'name', 'email', 'password', 'country', ]; |
Once we get this done, we already have everything, it will save in the database when we register and we can access it from the user object or from auth object.
1 |
Auth::user()->country |
Check if the user who is logged in is active
If we want only to be able to login active users, first of all we have to create a column that is to know if it is active or not.
1 |
php artisan make:migration add_active_field_to_user_table |
Open the file and add this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddActiveFieldToUserTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function(Blueprint $table) { $table->boolean('active')->default(1); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function(Blueprint $table) { $table->dropColumn('active'); }); } } |
Execute it:
1 |
php artisan migrate |
We already have the column in the database, now only is missing the checking that we have to put it in the controller of the login. We add the following code that will do the verification that the user is active:
1 2 3 4 5 6 7 8 |
// arriba del archivo use Illuminate\Http\Request; protected function credentials(Request $request) { $request['active'] = 1; return $request->only($this->username(), 'password', 'active'); } |
Now wehe we try to log in with a inactive user, Laravel will display a error message. These checks also could be applied for other types of things.
In the next post, we will use the middleware to restrict the access to some page if you are not logged in and we will explain how to create a custom one.