提问者:小点点

Laravel 5身份验证::尝试始终返回false


我试图编写登录系统,但Auth::trunt始终向我返回false,我的代码:

路线:

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

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

登录控制器:

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";
            }
        }

}
?>

用户模型:

<?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'];
}

?>

登录视图:

@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记录表结构

我在Cloud9IDE上开发这个,搜索了很多帖子,还是找不到问题,是密码散列还是什么问题?顺便说一句,我检查了表单输入是否可以接收。


共3个答案

匿名用户

你可以试试这种方式...

注册用户时,请在将密码发送到数据库之前使用bcrypt加密密码

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

而且因为您使用的是用户名,而不是默认的电子邮件

您的登录控制器。。

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";
            }
        }
}

希望这能奏效。

匿名用户

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

在保存用户记录时,将上述突变体添加到您的用户模型中以散列密码。Auth::attempt()需要散列密码

匿名用户

我查看了您的数据库记录,发现您在保存时没有对密码进行哈希运算。Auth::trunt()正在检查哈希密码,而您保存的密码为纯文本。