How To Customize Laravel Auth.

How To Customize Laravel Auth.

Authentication is the process of validating the user details in web application. Laravel makes it very simple in fact every thing, related to authentication that is Login,Registration , Forgot password, Remember me is already configured . We do not need to create these things manually in Laravel web application if we use Laravel auth.

Laravel provides basic login,registration and password reset functionalities . These things can be easily customize . So in this tutorial we are going to learn how to customize basic laravel registration.We will add Mobile Number and Upload image fields in addition to existing fields.

So lets start , how we can customize Laravel authentication. I assume you have already installed Laravel . If not install follow below link https://laravel.com/docs/7.x/installation.

Run below command to create form and associated controller to perform authentication.

composer require laravel/ui

php artisan ui vue --auth

npm install

npm run dev

This command will create all the associated controller,model,view,migration and routes. this is preview image of default registration page.

Add mobile number and image fields in user table 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->id();
            $table->string('name');
            $table->string('email')->unique();
	     $table->integer('mobile_no');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Update fillable properties in User model.

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','mobine_no','image',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Update Registration controller file.

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
	  
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255','min:3'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
			'mobine_no' => ['required','min:10'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
			'image' => 'required|image|mimes:jpg,jpeg',
        ],
		[ 'name.required' => 'The :attribute field can not be blank value.']
		
		);
		 
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
	 
		$imageName = time() . '.' . $data['image']->getClientOriginalExtension();
				$data['image']->move(
				base_path() . '/public/upload/', $imageName
			);
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
			'mobine_no'=>$data['mobine_no'],
			'image' => $imageName,
            'password' => Hash::make($data['password']),
        ]);
    }
}

Modify registration blade file .

<div class="form-group row">
                            <label for="mobine_no" class="col-md-4 col-form-label text-md-right">{{ __('Mobile Number') }}</label>

                            <div class="col-md-6">
                                <input id="mobine_no" type="text" class="form-control @error('mobile_no') is-invalid @enderror" name="mobine_no" value="{{ old('mobine_no') }}"   autocomplete="mobile_no">

                              @if ($errors->has('mobine_no'))
                                    <span class="text-danger">
                                        <strong>{{ $errors->first('mobine_no') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

<div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Upload Image') }}</label>

                            <div class="col-md-6">
                                <input id="image" type="file"  name="image">
                            </div>
                        </div>

After that registration page looks like as below

Exit mobile version