Laravel 5.4自定义Auth ::尝试总是返回false,即使我的凭证是真的

时间:2017-03-16 16:56:31

标签: authentication laravel-5 laravel-5.4

我尝试创建auth :: guard(' profile')并使用个人资料表和个人资料模型,但当我尝试执行Auth :: attempt($ credential)时,它总是返回false即使我的凭据是真的。

移植

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('ProfileID');
            $table->string('Name');
            $table->string('UserId')->unique();
            $table->string('Email')->unique();
            $table->string('Password');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}

模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class Profile extends Authenticatable
{
    use Notifiable;

    protected $fillable =
    [
        'Name', 'UserId', 'Email', 'Password'
    ];

    public $timestamps = false;

    protected $hidden =
    [
        'Password'
    ];
}

设置\ auth.php

<?php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'profile' => [
            'driver' => 'session',
            'provider' => 'profiles'
        ]
    ],   

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

        'profiles' => [
            'driver' => 'eloquent',
            'model' => App\Profile::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

ProfileController可

public function postLogin(Request $r){
        $credential = array(
            'UserId' => $r->input('UserId'),
            'Password' => $r->input('Password')
        );

        if(Auth::guard('profile')->attempt($credential)) {
          echo "success";
        }else{
            Session::flash('error', 'Username or password is incorect');
            return redirect('/');
        }
    }

请帮助我,我无法找到问题所在。

0 个答案:

没有答案