Monday, March 2, 2020

Laravel Authentication with Email or Username Tutorials

Laravel Authentication with Email or Username Tutorials


Let's start Laravel Authentication with Email or Username Tutorial.I'll give you a simple Laravel 6 login solution with username or email for authentication. It is easy to log into the Laravel 6 application with username and email address authentication.

At some point we need to create a login page with a username or email address to login. It is a great feature if you have it on your website as it is really easy to remind someone of your customer. Customer If you have forgotten the email, you have a user name. So it really helps.

In Laravel 6 Authentication with Email or Username Tutorial I will explain step by step how you can configure the login with username or email. So let's follow the next steps.

Step 1: Install Laravel 6


In Laravel Authentication with Email or Username Tutorial.First, we need to get a new version of the Laravel 6 application using the following command. So open your terminal OR the command prompt and run the following command:

composer create-project --prefer-dist laravel/laravel blog


Step 2: Install Laravel User Interface


There are a few steps you need to take to authenticate in your Laravel 6 application.

You must first install the Laravel / UI package as shown below:

composer require laravel/ui


Step 3: Generate an authentication framework


Here we have to generate authentication frameworks in Laravel 6 with the command laravel ui. Then we generate with the following command:

php artisan ui bootstrap --auth

Now you need to run the npm command, otherwise you won't see a better design of the login and registration page.

Install NPM:


npm install

Run NPM:


npm run dev


Step 4: Add Username Column


Now add a new column username to your user table so that you can easily update your migration as given below.

migration


<?php

  

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

  

class CreateUsersTable extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::create('users', function (Blueprint $table) {

            $table->bigIncrements('id');

            $table->string('name');

            $table->string('email');

            $table->string('username')->nullable();

            $table->timestamp('email_verified_at')->nullable();

            $table->boolean('is_admin')->nullable();

            $table->string('password');

            $table->rememberToken();

            $table->timestamps();

        });

    }

  

    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::dropIfExists('users');

    }

}

Now you can start the migration

php artisan migrate


Step 5: Update Login View


Laravel has created a standard logon blade file. We need to add the comman username field and delete the email field. So let's update as follows:

resources/views/auth/login.blade.php
....

<div class="form-group row">

    <label for="username" class="col-md-4 col-form-label text-md-right">Username Or Email</label>

   

    <div class="col-md-6">

        <input id="username" type="username" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required  autofocus>

   

        @error('username')

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

                <strong>{{ $message }}</strong>

            </span>

        @enderror

    </div>

</div>

....


Step 6: Overwrite Login method


Now we have to overwrite the login with the LoginController file. So let's add the login controller as shown below:

app/Http/Controllers/Auth/LoginController.php
<?php

  

namespace App\Http\Controllers\Auth;

  

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

  

class LoginController extends Controller

{

  

    use AuthenticatesUsers;

    

    protected $redirectTo = '/home';

    /**

     * Create a new controller instance.

     *

     * @return void

     */

    public function __construct()

    {

        $this->middleware('guest')->except('logout');

    }

  

    /**

     * Create a new controller instance.

     *

     * @return void

     */

    public function login(Request $request)

    {   

        $input = $request->all();

  

        $this->validate($request, [

            'username' => 'required',

            'password' => 'required',

        ]);

  

        $fieldType = filter_var($request->username, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

        if(auth()->attempt(array($fieldType => $input['username'], 'password' => $input['password'])))

        {

            return redirect()->route('home');

        }else{

            return redirect()->route('login')

                ->with('error','Email-Address And Password Are Wrong.');

        }

          

    }

}


You can add some dummy records to your user table.

Now you can run your project.


I hope this Tutorial of Laravel Authentication with Email or Username could help you ...

Thanks.


0 comments:

Post a Comment

Please don't enter any spam link in the comment box.