Laravel Auth 尝试不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45603185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:28:57  来源:igfitidea点击:

Laravel Auth attempt not working

laravellaravel-5

提问by Nihal Saxena

First i created the Admin model in which i implemented it using Authenticable Admin.php

首先,我创建了 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;
    }

then i created the migration :

然后我创建了迁移:

    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');
        }
    }

Replaced the Users model in config/auth.php with Admin Model :

将 config/auth.php 中的 Users 模型替换为 Admin Model :

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

AdminController.php

管理控制器.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

网页.php

Route::get('/admin/login',[
    'uses'=>'AdminController@getLogin',
    'as'=>'admin.login'
]);

Route::post('/admin/login',[
    'uses'=>'AdminController@postLogin',
    'as'=>'admin.login'
]);

login.blade.php

登录名.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>

so when i tried to login and dd the Auth Attempt it is returning false i have a user with email [email protected] and password test : enter image description here

因此,当我尝试登录并添加 Auth Attempt 时,它返回 false 我有一个用户使用电子邮件 [email protected] 和密码 test : 在此处输入图片说明

created the user using seeder :

使用 Seeder 创建用户:

<?php

use Illuminate\Database\Seeder;

class AdminTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $admin = new \App\Admin;
        $admin->email='[email protected]';
        $admin->password='[email protected]';
        $admin->save();
    }
}

回答by Wreigh

So for your seeder.

所以对于你的播种机。

$admin = new \App\Admin;
$admin->email='[email protected]';
$admin->password='[email protected]';
$admin->save();

the password should be hashed using bcrypt.

密码应该使用 bcrypt 散列。

$admin->password = bcrypt('[email protected]');

but as you've said, you're trying 'test' as the password, so it should be:

但正如您所说,您正在尝试将“测试”作为密码,所以它应该是:

$admin->password = bcrypt('test');

that should work.

那应该工作。

the reason is, Auth::attempt hashes the entered password using bcrypt. So if the stored password in your database is not bcrypt-ed, Auth::attempt will fail to match the two passwords.

原因是,Auth::attempt 使用 bcrypt 散列输入的密码。因此,如果数据库中存储的密码不是 bcrypt-ed,则 Auth::attempt 将无法匹配两个密码。

For example you put test as the password, plain text, if you try to login to laravel using Auth::attempt using password = 'test', string 'test' will be compared against the bcrpyt version of 'test'.

例如,您将 test 作为密码,纯文本,如果您尝试使用 Auth::attempt 使用 password = 'test' 登录 Laravel,则字符串 'test' 将与 bcrpyt 版本的 'test' 进行比较。