jeudi 14 février 2019

Laravel auth attempt not working or returning false always?

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',
]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire