ReakyMark
1 year ago

How can i use name field to login instead of email field in Laravel auth ?

Posted 1 year ago by ReakyMark

I have override in Login Controller but it still not log me in except i switch to use email. because my old DB is use name so i doesn't register with email that the reason i want to change email to name for login.

my Admin\Login

use AuthenticatesUsers;

    protected $redirectTo = '/admin/dashboard';

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

    /*
     * Override default config
     */

    protected $username = 'name';
    
    public function showLoginForm()
    {
        return view('auth.admin.login');
    }

    public function username()
    {
        return 'name';
    }

my login blade

<form method="post" action="{{ route('admin.login') }}">
                    {{ csrf_field() }}
                    <input type="text" name="name" placeholder="Username">
                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif

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

                    <input type="submit" name="but_login" value="Login" class="loginmodal-submit">

                </form>

this is my custom route for admin login, Route.php

Route::group([
    'prefix' => 'admin',
    'as' => 'admin'
], function () {
    require base_path('routes/admins/web.php');
    require base_path('routes/logins/web.php');
});

this is my logins/webs.php

Route::get(
    'login',
    'Auth\Admin\[email protected]'
)->name('.login');

Thank in advance.

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