0

I have a custom table for users named 'iw_users' and inside, i have also a custom column for email and password named 'iu_email' and 'iu_password'. What i want to do here is to set my iu_email as the email and iu_password as a password on login, register, and forgot password (basically all functionalities that laravel Auth provides).

I have followed the answer on this thread: Laravel: How can i change the default Auth Password field name

But when i try to login (with correct credentials) the page is just refreshing and redirecting me again to log in page (I can confirm that laravel did not authorize me using Auth::check()).

Also, the color of the error and input are not red whenever i try to login with wrong credentials.

This is my App\User code:

class User extends Authenticatable
{
    use Notifiable;


    protected $table = 'iw_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'iu_user_id', 'iu_last_name', 'iu_first_name', 'iu_email', 'iu_mobile_no', 'iu_password', 'iu_gender', 'iu_country', 'iu_role', 'iu_photo', 'iu_status', 'iu_ipaddress'
    ];


    public function getAuthPassword()
    {
        return $this->iu_password;
    }

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    // Commented. i don't have 'password' and 'remember_token' in iw_users
    // protected $hidden = [
    //     'password', 'remember_token',
    // ];
}

This is my LoginController.php

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

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

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

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

This is my login.blade.php form

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

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

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

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

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

        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password" required>

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

    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
                </label>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-8 col-md-offset-4">
            <button type="submit" class="btn btn-primary">
                Login
            </button>

            <a class="btn btn-link" href="{{ route('password.request') }}">
                Forgot Your Password?
            </a>
        </div>
    </div>
</form>
DsRaj
  • 2,288
  • 1
  • 16
  • 26
boosted_duck
  • 478
  • 3
  • 15
  • 1
    You should not use the bydefault auth controllers if you can change the users table columns ..... you can make own controller and also change the routes. – Inzamam Idrees Jan 16 '19 at 05:20
  • you need to update the config/auth.php file as per your change in table specially for password – DsRaj Jan 16 '19 at 05:26

1 Answers1

0

I would suggest you to check https://scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel

Have you got any error while debugging this? I doubt, it's unable to override their default fields.