Auth :: attempt()仅在数据库行1上返回true

时间:2014-01-09 09:47:58

标签: php authentication laravel laravel-4

我的Laravel-4 Authentication有一个奇怪的问题。只有我的第一个数据库行可以登录我的Laravel项目,其他数据库返回false。

我使用的身份验证与预期有点不同。我没有用户名,只有密码,也称为“代码”。我也尝试使用用户名(作为“派对”),但这似乎也不起作用。

我在我的数据库中添加了一个表“password_unh”(=密码未散列),当然这不是最终应该如何,但是要检查我的密码(因为我的密码表是经过哈希处理的),而我也把这看作是我的“派对”。

这是我的代码(我在评论中放了关于'party'的代码)

login.blade.php

@extends("layout")
@section("content")
    {{ Form::open([
        "route"        => "party/login",
        "autocomplete" => "off"
    ]) }}
        <!--{{ Form::label("party", "Party") }}
        {{ Form::text("party", Input::get("party"), [
            "placeholder" => "Christmas party Smith"
        ]) }}-->
        {{ Form::label("code", "Code") }}
        {{ Form::password("code") }}

        @if($errors->has())
            @foreach ($errors->all() as $error)
                <div class='error'>{{ $error }}</div>
            @endforeach
        @endif
        {{ Form::submit("login") }}
    {{ Form::close() }}
@stop
@section("footer")
    @parent
    <script src="//polyfill.io"></script>
@stop

UserController.php

<?php
use Illuminate\Support\MessageBag;
class UserController extends Controller
{
    public function loginAction() {

        $errors = new MessageBag();
        if ($old = Input::old("errors"))
        {
            $errors = $old;
        }
        $data = [
            "errors" => $errors
        ];
        if (Input::server("REQUEST_METHOD") == "POST")
        {
            $validator = Validator::make(Input::all(), [
                //"party" => "required",
                "code" => "required"
            ]);

            if ($validator->passes())
            {
                $credentials = [
                    //"password_unh" => Input::get("party"),
                    "password" => Input::get("code")
                ];
                if (Auth::attempt($credentials))
                {
                    return Redirect::to('profile');
                }
            }
            $data["errors"] = new MessageBag([
                "code" => [
                    "Party and/or code invalid."
                ]
            ]);
            //$data["party"] = Input::get("party");

            return Redirect::route("party/login")
                ->withInput($data);
        }
        return View::make("party/login", $data);
    }
}

数据库用户表 enter image description here

2 个答案:

答案 0 :(得分:0)

Actualy,您应该在username数组中至少包含password$credentials字段。 我也可以看到你在数据库表中也错过了它。

答案 1 :(得分:0)

这不太理想,但你可以做到

$user = User::where('password',Hash::make(Input::get('code')));

if(Auth::loginUsingId($user->id))
{
   //do  blah
}

if(Auth::login($user))
{
   //do  blah
}