简体   繁体   English

Laravel 5 Auth::attempt 总是返回 false

[英]Laravel 5 Auth::attempt always return false

I tried to write an login system but Auth::attempt always return false to me, my code:我试图编写一个登录系统,但 Auth::attempt 总是向我返回 false,我的代码:

route:路线:

    Route::get('/login', function () {
    return view('login');
});

    Route::post('/loginProcess', 'LoginController@login');

LoginController:登录控制器:

namespace App\Http\Controllers;

use Request;
use Auth;
use App\Http\Requests;
use Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
class LoginController extends Controller
{
 public function login(){
        $uname = Request::get('username');
        $password = Request::get('password');
            if (Auth::attempt(array('username' => $uname, 'password' => $password))){
                return "success";
            }
            else {        
                return "Wrong Credentials";
            }
        }

}
?>

User model:用户模型:

<?php
namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract { 
    use Authenticatable, CanResetPassword;
    protected $table = 'user';
    public $timestamps = false;
    protected $fillable = ['username', 'password'];
}

?>

Login view:登录视图:

@extends('main')

@section('content')
    {!! Form::open(['url' => '/loginProcess']) !!}
        <div class="form-group">
            {!! Form::label('username', 'Username:') !!}
            {!! Form::text('username', null, ['class' => 'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('password', 'Password:') !!}
            {!! Form::password('password', null, ['class' => 'form-control']) !!}
        </div>

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

        <div class="form-group">
            {!! Form::submit('Login', ['class' => 'btn btn-primary form-control']) !!}
        </div>
    {!! Form::close() !!}
@stop

DB records Table structure DB记录表结构

I am developing this on cloud9 IDE, and searched/goggle many posts, still can't find out the problem, is it the problem of password hash or sth?我在cloud9 IDE上开发这个,搜索了很多帖子,还是没找到问题,是密码hash的问题还是sth的问题? Btw, I checked that the form input can be received.顺便说一句,我检查了可以接收表单输入。

public function setPasswordAttribute($value) {
    $this->attributes['password'] = bcrypt($value);
}

add above mutator to your user model to hash password when you are saving user records.当您保存用户记录时,将上述修改器添加到您的用户模型以散列密码。 Auth::attempt() requires hashed passwords Auth::attempt()需要散列密码

You can try this way..你可以试试这个方法。。

When you registering user use bcrypt to encrypt your password before seving them to database当您注册用户时,使用 bcrypt 加密您的密码,然后再将它们发送到数据库

$password = bcrypt($input['password']);
            // OR
$psssword = bcrypt(Request::get('password'));

And because you are using username in place of default email并且因为您使用username代替默认email

Your LoginController..您的登录控制器..

namespace App\Http\Controllers;

use Request;
use Auth;
use App\Http\Requests;
use Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
class LoginController extends Controller
{

 use AuthenticatesAndRegistersUsers;

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

 public function login(){
        $uname = Request::get('username');
        $password = Request::get('password');
            if (Auth::attempt(array('username' => $uname, 'password' => $password))){
                return "success";
            }
            else {        
                return "Wrong Credentials";
            }
        }
}

Hope this will work.希望这会奏效。

I looked at your DB records and found that you are not hashing your password while saving.我查看了您的数据库记录,发现您在保存时没有对密码进行哈希处理。 Auth::attempt() are checking against hashed password while your saved passwords are in plain text. Auth::attempt()正在检查散列密码,而您保存的密码是纯文本。

Rename Your Password Field
try to debug the $query in \Illuminate\Auth\DatabaseUserProvider\ 



public function retrieveByCredentials(array $credentials)
{
    // First we will add each credential element to the query as a where clause.
    // Then we can execute the query and, if we found a user, return it in a
    // generic "user" object that will be utilized by the Guard instances.
    $query = $this->conn->table($this->table);

    foreach ($credentials as $key => $value)
    {
        if ( ! str_contains($key, 'password'))
        {
            dd($query->where($key, $value));

        }
    }

    // Now we are ready to execute the query to see if we have an user matching
    // the given credentials. If not, we will just return nulls and indicate
    // that there are no matching users for these given credential arrays.
    $user = $query->first();

    return $this->getGenericUser($user);
}

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

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