Integration and Customization of Laravel Authentication

Published On: 1 May 2019.By .
  • General

1. For integrating Laravel Authentication, you should run the following artisan  command

php artisan make:auth 

it will integrate Laravel Default Authentication. If you looked at folder structure following files will generated

 Controller Files

App -> http -> controller -> Auth

ForgetPassword.php

LoginController.php

RegisterController.php

ResetPassword.php

VerificationController.php

Model Files

App 

User.php

View Files

resources -> views -> auth

login.blade.php

register.blade.php

verfiy.blade.php

resources -> views -> auth ->password

email.blade.php

reset.blade.php

2. Customaization in Laravel Authentication

Auth View

all the auth files are extend by app.layout file

path –  resource-> view -> layouts -> app.blade.php

there are sevral things you can do with app.blade.php

1. change the app name –

in the default Laravel Auth contains app name as a “Laravel” if want to change the app name then you should do following things 

find –

{{config(‘app.name’, ‘Laravel’)}}

app.name is define in config file as ‘name’ -> ‘Laravel’

lets change ‘Laravel’ to ‘DemoProject’

It will change project name Laravel to DemoProject

Configured Reset Password

For using Laravel Reset Password functionality you have to do following changes in .env file

1. find MAIL_DRIVER change it to MAIL_DRIVER = smtp

2. find MAIL_HOST change it yo MAIL_HOST = smtp.gmail.com

3. find MAIL_USERNAME change it to MAIL_USERNAME = example.gmail.com

4. fine MAIL_PASSWORD change it to MAIL_PASSWORD = your_password

Then restart your server using following command 

php artisan serve

Login Controller

1. redirect changes

by default after successfully it will redirect to the home page using the 

protected $redirectTo = ‘/home’;

we want redirected to the welcome page after login we change to

protected $redirectTo = ‘/’;

All the Login Function are define in the 

vender\laravel\framework\src\illuminate\foundation\Auth\AuthenticationUser.php

2. login Using Name and password

by default laravel has login using Email and Password, if in some cases we need to login using any other fields like name,password or name,email then we can do this using following

default condition-

when we put our credentials in inputbox or login page following function get there values using Request

protected function validateLogin(Request $request)

    {

        $request->validate([

            $this->username() => ‘required|string’,

            ‘password’ => ‘required|string’,

        ]);

    }

and this username() function is return email

public function username()

{

     return ’email’;

}

here we need to change email to name for login using name

public function username()

{

     return ‘name’;

}

3. Login Throttling

If you are using Laravel’s built-in LoginController class, the Illuminate\Foundation\Auth\ThrottlesLogins trait will already be included in your controller. By default, the user will not be able to login for one minute if they fail to provide the correct credentials after several attempts.

Default condition-

public function maxAttempts()

{

        return property_exists($this, ‘maxAttempts’) ? $this->maxAttempts : 5;

}

this mean we have only 5 unsuccessfull attemps for login

public function decayMinutes()

{

        return property_exists($this, ‘decayMinutes’) ? $this->decayMinutes : 1;

}

after using all attempt account is locked for 1 minute

we can change numer of attemps and time by chnaging this:

For Number of Attempts change in function maxAtttempts() 

$this->maxAttempts : 10; // Now you have 10 Attempts

For change the time change in function decayMinutes()

$this->decayMinutes : 2; // now account will locked for 2 minutes

Customization in Register Controller

1. Add New Field in Register Page- In the following step we are going to add a new Last Name field in Register form

step-1 goto register.blade.php

-find the name field and put the Last Name Field

<div class=”form-group row”>

<label for=”name” class=”col-md-4 col-form-label text-md-right”>{{ __(‘Last Name’) }}</label>

            <div class=”col-md-6″>

                       <input id=”name” type=”text” class=”form-control{{ $errors->has(‘name’) ? ‘ is-invalid’ : ” }}” name=”last_name” value=”{{ old(‘name’) }}” required autofocus>

                      @if ($errors->has(‘name’))

                           <span class=”invalid-feedback” role=”alert”>

                                 <strong>{{ $errors->first(‘name’) }}</strong>

                            </span>

                       @endif

                  </div>

   </div>

2. Add column in Database

All the user data is stored in users table, so we have to add a new column in users table

– Run migration command

php artisan make:migration add_lastName_inUsers  –table=users

-find the  add_lastName_inUsers migration file under database-> migration folder and add new column 

public function up()

{

Schema::table(‘users’, function (Blueprint $table) 

{

$table->string(‘last_name’)->after(‘name’);

});

}

after updating up() function run migrate command

php artisan migrate

it will create new column in users table after the name column

3.Update User model – 

Update User model using this

protected $fillable = [‘name’,’last_name’,’email’,’password’];

4.Updates in RegisterController-

-add validation

protected function validator(array $data)

{

return Validator::make($data, [

‘name’ => [‘required’, ‘string’, ‘max:255’],

‘last_name’ => [‘required’, ‘string’, ‘max:255’],

’email’ => [‘required’, ‘string’, ’email’, ‘max:255’, ‘unique:users’],

‘password’ => [‘required’, ‘string’, ‘min:6’, ‘confirmed’],

]);

}

– Get Values from frontend and save in database

protected function create(array $data)

{

$user = User::create

([

‘name’ => $data[‘name’],

‘last_name’ => $data[‘last_name’],

’email’ => $data[’email’],

‘password’ => bcrypt($data[‘password’]),

]);

}

  ->name(‘password.request’);

Related content

That’s all for this blog

Go to Top