Laravel 4验证凭据无法正常工作

时间:2014-12-03 17:11:28

标签: php login laravel-4 credentials

我已经跟着一个用laravel 4创建登录的例子,但是我有问题,如果凭据是正确的,它也一直说“凭证无效”。这是我的代码

user.php的     

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

class User extends Eloquent implements UserInterface, RemindableInterface{
    protected $table = "user";
    protected $hidden = ["password"];

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

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

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return "remember_token";
    }

    public function getReminderEmail()

    {
        return $this->email;
    }
}

UserController.php

<?php

class UserController extends Controller
{
    public function login()
    {
        if ($this->isPostRequest()) {
            $validator = $this->getLoginValidator();

            if ($validator->passes()) {

                $credentials = $this->getLoginCredentials();

                if (Auth::attempt($credentials)) {
                    print_r($credentials);
                    return Redirect::route("user/profile");
                }

                return Redirect::back()->withErrors([
                    "password" => ["Credentials invalid."]
                    ]);
            } else {
                return Redirect::back()
                ->withInput()
                ->withErrors($validator);
            }
        }

        return View::make("user/login");
    }

    protected function isPostRequest()
    {
        return Input::server("REQUEST_METHOD") == "POST";
    }

    protected function getLoginValidator()
    {
        return Validator::make(Input::all(), [
            "username" => "required",
            "password" => "required"
            ]);
    }

    protected function getLoginCredentials()
    {
        return [
        "username" => Input::get("username"),
        "password" => Input::get("password")
        ];
    }
}

login.blade.php

@extends("layout")
@section("content")
{{ Form::open() }}
{{ $errors->first("password") }}<br />
{{ Form::label("username", "Username") }}
{{ Form::text("username", Input::old("username")) }}
{{ Form::label("password", "Password") }}
{{ Form::password("password") }}
{{ Form::submit("login") }}
{{ Form::close() }}
@stop
有人可以帮帮我吗?

0 个答案:

没有答案