tareenmj
2324
13
Laravel

How to use Authentication using Username instead of email

Posted 1 year ago by tareenmj

I want to create an app where I Log in the username based on username, instead of email address. Since I am a beginner to Authentication, I simply ran the php artisan make:auth and the migrate commands. I made no change to the user migration file or any other.

I edited my Login controller by placing a method as such:

class LoginController extends Controller
{

use AuthenticatesUsers;
    
    public function username(){
        return 'username';
    }

//the rest of the controller code

Now my Login Blade file is as:

<form class="form-horizontal" method="POST" action="{{ route('login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Username</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

//followed by the rest of the html.

However, as soon as I type in the name and the password, it doesn't allow me to be logged in, and does nothing. I am unsure about what I am doing wrong here, can somebody please help?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.