简体   繁体   English

laravel 5身份验证始终返回false

[英]laravel 5 Authentication always return false

i have tried everything, i really don't know where i am doing wrong but each time i log in, it returns false. 我已经尝试了一切,但我真的不知道我在哪里做错了,但是每次我登录时,它都会返回false。 here is the method to store users in the db: 这是将用户存储在数据库中的方法:

public function store(CreateUserRequest $request)
    {
        $user = new User();

        $password = str_random(10);
        $password_hash = bcrypt($password);

        $new_user = $user->create([
            'first_name'=>$request->input('first_name'),
            'last_name'=>$request->input('last_name'),
            'email'=>$request->input('email'),
            'fone_number'=>$request->input('fone'),
            'password'=>$password_hash
        ]);

        $new_user->permissions()->create(UserPermission::$default);
        //send email to the user
        Mail::send('admin.emails.auth.new_user',['new_user'=>$new_user,'password'=>$password],function($message) use ($new_user){
            $message->to($new_user->email)
                    ->from('lilgaetan88@gmail.com','Admin Pedagogia')
                    ->subject("your password");
        });

        return redirect()->route('home')->with('Success',"mail sent");

    }

the logincontroller 登录控制器

public function postLogin(LoginRequest $request){

        $remember = ($request->has('remember')) ? true : false;

         if(Auth::attempt($request->only(['email','password']),$remember)){
             return redirect()->intended('/home');
        }

           return redirect()->route('login')->with('fail','email or password incorrect');
    }
}

login.blade.php file: login.blade.php文件:

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

            @if(Session::has('fail'))
                <div class="alert alert-danger">{{\Illuminate\Support\Facades\Session::get('fail')}}</div>
            @endif

            @if ($errors->has())
                <div class="alert alert-danger">
                    @foreach ($errors->all() as $error)
                        {{ $error }}<br/>
                    @endforeach
                </div>
            @endif

        <div class="login-wrap">
            <p class="login-img"><i class="fa fa-lock"> Login Page</i></p>
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                <input type="text" name="email" class="form-control" placeholder="E-mail" autofocus>
            </div>
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-key"></i></span>
                <input type="password" name="password" class="form-control" placeholder="Password">
            </div>
            <label class="checkbox">
                <input type="checkbox" name="remember"> Se souvenir de moi
                <span class="pull-right "> <a href="#"> Mot de Passe Oublie?</a></span>
            </label>
            {!!Form::token()!!}
            <button class="btn btn-primary  btn-block" type="submit">Login</button>
        </div>
    </form>

when i dd(Hash::check($pssowrd,$new_user->password)), it returns always true, that means the hashed password was correct but the authentication still returns false 当我dd(Hash :: check($ pssowrd,$ new_user-> password))时,它始终返回true,这意味着哈希密码是正确的,但身份验证仍然返回false

尝试在第6行删除remove bcrypt()方法。我认为密码散列是在laravel中自动处理的,因此您不需要bcrypt($password)

One possible Error would be, you are supposed send an Associative array of data to the attempt method; 一种可能的错误是,您应该将一个关联数据数组发送给try方法; not an indexed array. 不是索引数组。 So i guess it would work fine if you did like this, 所以我想如果您这样做的话会很好用的,

    Auth::attempt(['email' => $request['email'], 'password' => $request['password']], $remember))

Indexed and Associative Arrays in Php Php中的索引数组和关联数组

Laravel Doc Auth::attempt() Laravel Doc Auth :: attempt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM