首先我创建了Admin模型,我在其中使用Authenticable Admin.php
实现了它<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
class Admin extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
//
use Authenticatable;
}
然后我创建了迁移:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->rememberToken();
$table->string('email');
$table->string('password');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('admins');
}
}
使用Admin Model:
替换config / auth.php中的Users模型'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
AdminController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
class AdminController extends Controller{
public function getIndex()
{
$posts= Post::orderBy('created_at','desc')->take(3)->get();
return view('backend.index')->with(['posts'=>$posts]);
}
public function getLogin(){
return view('backend.login');
}
public function postLogin(Request $request){
$this->validate($request,[
'email'=>'required|email',
'password'=>'required'
]);
dd(Auth::attempt(['email'=>$request['email'],'password'=>$request['password']]));
if(!Auth::attempt(['email'=>$request['email'],'password'=>$request['password']])){
return redirect()->back()->with(['fail'=>'Could Not Log You In']);
}
return redirect()->route('admin.index');
}
public function getLogout(){
Auth::logout();
return redirect()->route('blog.index');
}
}
web.php
Route::get('/admin/login',[
'uses'=>'AdminController@getLogin',
'as'=>'admin.login'
]);
Route::post('/admin/login',[
'uses'=>'AdminController@postLogin',
'as'=>'admin.login'
]);
login.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Area</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{URL::to('css/bootstrap.min.css')}}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="{{URL::to('js/bootstrap.min.js')}}"></script>
@yield('styles')
</head>
<body>
<div class="container">
<div class="container-fluid">
@include('includes.info-box')
<form method="post" action="{{route('admin.login')}}">
<div class="form-group">
<label for="email">Email: </label>
<input type="email" class="form-control" id="email" name="email"
{{$errors->has('email')?'class=alert alert-danger':'' }}
value="{{Request::old('email')}}">
</div>
<div class="form-group">
<label for="password">Password: </label>
<input type="password" class="form-control" id="password" name="password"
{{$errors->has('password')?'class=alert alert-danger':'' }}
>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Login</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</div>
</form>
</div>
</div>
</body>
</html>
因此,当我尝试登录并且dd时,Auth Attempt返回false我有一个用户使用电子邮件test@123.com和密码测试:
使用播种机创建用户:
<?php
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$admin = new \App\Admin;
$admin->email='test@123.com';
$admin->password='test@123.com';
$admin->save();
}
}
答案 0 :(得分:3)
所以对于你的播种机。
$admin = new \App\Admin;
$admin->email='test@123.com';
$admin->password='test@123.com';
$admin->save();
密码应使用bcrypt进行哈希处理。
$admin->password = bcrypt('test@123.com');
但正如你所说的那样,你正在尝试“测试”&#39;作为密码,所以它应该是:
$admin->password = bcrypt('test');
应该有用。
原因是,Auth ::尝试使用bcrypt哈希输入的密码。 因此,如果数据库中存储的密码不是bcrypt-ed,则Auth :: attempt 将无法匹配这两个密码。
例如,如果您尝试使用Auth :: try尝试使用密码=&#39; test&#39;,字符串&#39; test&#39;,则将test作为密码,纯文本。将与bcrpyt版本的&#39; test&#39;进行比较。