how to change email authentication by username using laravel?

Viewed 28

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 class="d-flex justify-content-center form_container">
                <form method="POST" action="{{ route('login') }}">
                    @csrf
                        
                        
                    <div class="input-group mb-3">
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="fas fa-user"></i></span>
                        </div>
                        <input type="text" value="FZ DIGITAL" name="username" id="inputEmail" class="form-control input_user" placeholder="username" autocomplete="username" required autofocus>
                        @error('username')
                            <div class="alert alert-danger" role="alert">
                                <strong>{{ $message }}</strong>
                            </div>
                        @enderror
                    </div>
                    <div class="input-group mb-2">
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <i class="fas fa-key"></i>
                            </span>
                        </div>
                        <input type="password" name="password" id="inputPassword" class="form-control input_pass" autocomplete="current-password" required>
                        @error('password')
                            <div class="alert alert-danger" role="alert">
                                {{ $message }}
                            </div>
                        @enderror
                    </div>
                    
                    <div class="d-flex justify-content-center mt-3 login_container">
                        <button type="submit" name="button" class="btn login_btn">Connexion</button>
                    </div>
                </form>
            </div>
1 Answers

Authentication by email

Auth::attempt([ 'email' => 'email@example.com', '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