0

I've been looking all over the place (I checked all the other duplicate questions and answers ), no luck. I don't understand what am I doing wrong, all the values are coming from post, but Auth:attempt keeps on failing/returning false, if I try to implement login manually it won't authenticate as I am expecting, Also do I have to make or use separate methods like for validation, credentials, username ..etc ?

Here is my login Controller > login method

public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'usermail'    => 'required|max:255',
            'password'    => 'required_with:usermail',
        ],[
            'password.required_with'  => "The password field is empty."
        ]);
        if ($validator->fails()) {
            return redirect()
                        ->route('admin.login')
                        ->withErrors($validator)
                        ->withInput();
        }
        $usermail = $request->get('usermail');
        $password = $request->get('password');
        $remember = $request->get('rememberMe');

        if(filter_var($usermail, FILTER_VALIDATE_EMAIL)) {
            $isEmailExist = User::where('user_email',$usermail)->first();
            if($isEmailExist != null){
                if(Auth::attempt([
                        'user_email' => $usermail,
                        'user_pass'  => $password
                ])){
                    return redirect()->intended('admin/dashboard');
                }else{
                    return back()->with([
                        'message'   => '<strong>ERROR</strong>: The password you entered for the email address <strong>'.$usermail.'</strong> is incorrect. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                    ]);   
                }
            }else{
                return back()->with([
                    'message'   => '<strong>ERROR</strong>: Invalid email address.'
                ]);
            }
        }else{
            $isUsernameExist = User::where('user_login',$usermail)->first();
            if($isUsernameExist != null){
                if(Auth::attempt([
                        'user_login' => $usermail,
                        'user_pass'  => $password
                ],$remember)){
                    return redirect()->intended('admin/dashboard');
                }else{
                    return back()->with([
                        'message'   => '<strong>ERROR</strong>: The password you entered for the username <strong>'.$usermail.'</strong> is incorrect. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                    ]);
                }
            }else{
                return back()->with([
                    'message'   => '<strong>ERROR</strong>: Invalid username. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                ]);
            }
        }
    }

And this is my user migration schema,

Schema::create('vw_users', function (Blueprint $table) {
            $table->bigIncrements('ID');
            $table->string('user_login','60')->unique()->default('');
            $table->string('user_pass');
            $table->string('user_email','100')->unique()->default('');
            $table->rememberToken();
        });

Here is how i seed user,

User::create([
    'user_login'            => 'admin',
    'user_pass'             => Hash::make("123456"),
    'user_email'            => 'admin@gmail.com',
]);
Vipertecpro
  • 3,056
  • 2
  • 24
  • 43

1 Answers1

0

OK OK OK, I made it work, I think in laravel framework we can only create the column name for the password is "password" field in database authentication table.

I updated the following changes:-

  • I renamed the password field name from migration schema the "user_pass" to "password". (Also updated in login controller and user model).
  • Added following code into user model:-

    use Illuminate\Notifications\Notifiable;
    
    class User extends Authenticatable
    {
     use Notifiable;
     ...
    }
    

I checked twice to confirm so I revert back it didn't work.

If i make any sense to anyone please let me know and help me understand. I've looked into very similar posts like this Laravel: How can i change the default Auth Password field name can I please have a reference book or blog for all the laravel predefined libraries and functions? I know the vendor folder is full of surprises but still, I need more references.

Thank you so much for your time.

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43