Home > Net >  how to change email authentication by username using laravel?
how to change email authentication by username using laravel?

Time:09-22

I want to authenticate by username and password, I modify login.blade.php input name=email by name=username , and I don't know anything else to modify in LoginController.php

LoginController.php

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/events';

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

login.blade.php

<div >
                <form method="POST" action="{{ route('login') }}">
                    @csrf
                        
                        
                    <div >
                        <div >
                            <span ><i ></i></span>
                        </div>
                        <input type="text" value="FZ DIGITAL" name="username" id="inputEmail"  placeholder="username" autocomplete="username" required autofocus>
                        @error('username')
                            <div  role="alert">
                                <strong>{{ $message }}</strong>
                            </div>
                        @enderror
                    </div>
                    <div >
                        <div >
                            <span >
                                <i ></i>
                            </span>
                        </div>
                        <input type="password" name="password" id="inputPassword"  autocomplete="current-password" required>
                        @error('password')
                            <div  role="alert">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>
                    
                    <div >
                        <button type="submit" name="button" >Connexion</button>
                    </div>
                </form>
            </div>

CodePudding user response:

Authentication by email

Auth::attempt([ 'email' => '[email protected]', 'password' => 'password' ]);

Authentication by username

Auth::attempt([ 'username' => 'username', 'password' => 'password' ]);

Update

Illuminate\Foundation\Auth\AuthenticatesUsers.php

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username'; // <------- Change here
}
  • Related