我试着写一个登录系统,但Auth :: try总是向我返回false,我的代码是:
路线:
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";
}
}
}
?>
用户模型:
<?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
我在cloud9 IDE上开发这个,并搜索/调整了很多帖子,仍然无法找出问题,是密码哈希还是问题?顺便说一句,我检查了表格输入是否可以接收。
答案 0 :(得分:0)
我查看了您的数据库记录,发现您在保存时没有哈希密码。当您保存的密码是纯文本时, Auth :: attempt()正在检查哈希密码。
答案 1 :(得分:0)
angular.module('ui.bootstrap.demo', ['ui.bootstrap'])
.factory('modalFactory', function($uibModal) {
//Stuff
}
在保存用户记录时,将以上mutator添加到您的用户模型以哈希密码。 public function setPasswordAttribute($value) {
$this->attributes['password'] = bcrypt($value);
}
需要散列密码
答案 2 :(得分:0)
你可以尝试这种方式..
当您注册用户时,请使用bcrypt加密密码,然后再将其密封到数据库
$password = bcrypt($input['password']);
// OR
$psssword = bcrypt(Request::get('password'));
并且因为您使用username
代替默认email
您的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";
}
}
}
希望这会奏效。
答案 3 :(得分:0)
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);
}