Laravel Auth ::尝试失败

时间:2013-12-06 13:25:32

标签: php laravel-4

我正在尝试为管理员用户创建登录系统,但Auth ::尝试返回false ... 有没有人可以帮我这个? :p我已经关注了four.laravel.com的文档,但我似乎无法找到解决方案......

AdminController @ AUTH

public function auth()
{

    $rules = [
        'email'    => 'required|email',
        'password' => 'required',
    ];

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->passes()) {
        $credentials = [
            'email'      => Input::get('email'),
            'password'   => Input::get('password'),
            'deleted_at' => null, // Extra voorwaarde
        ];

        if (Auth::attempt($credentials)) {
            return Redirect::to('/');
        } else {
            return Redirect::route('admin.login')
                ->withInput()
                ->with('auth-error-message', 'U heeft een onjuiste gebruikersnaam of een onjuist wachtwoord ingevoerd.');
        }
    } else {

        return Redirect::route('admin.login') // Zie: $ php artisan routes
            ->withInput()             // Vul het formulier opnieuw in met de Input.
            ->withErrors($validator); // Maakt $errors in View.
    }
}

管理模型

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Admin extends Eloquent {
protected $table = 'admins';
    protected $softDelete = true;

    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at',
        'password'
    ];


    protected $fillable = [
        'id',
        'email',
        'approved'
    ];
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function getAuthPassword()
    {
        return $this->password;
    }
    public function getReminderEmail()
    {
        return $this->email;
    }

    public static function boot()
    {
        parent::boot();
        self::creating(function ($admin) {
            $admin->password = Hash::make($admin->password);
        });
    }

}

管理员迁移

    public function up()
{
    Schema::create('admins', function($table)
    {
        $table->increments('id');
        $table->string('email','255')->unique();
        $table->string('password','60');
        $table->timestamps();
        $table->softDeletes();
        $table->timestamp('approved')->nullable();
    });
}

提前致谢。 HS。

2 个答案:

答案 0 :(得分:1)

您的auth.php配置文件是否与您的不同型号/表格结构相匹配?

'model' => 'Admin';
'table' => 'admins';

您的管理模型中还没有protected $softDelete = true;,但您正在使用Auth::attempt();进行检查。请参阅http://laravel.com/docs/eloquent#soft-deleting

答案 1 :(得分:0)

确定。我终于弄清楚了自己。 我已将默认用户类的名称更改为“Admin”。由于laravel自动实现用户模型的UserInterface,我需要在Admin-model中手动实现它:

class Admin extends Eloquent implements UserInterface{
{
    ...
}

这就是它的全部......