2

我正在尝试以简单的方式进行 laravel 身份验证,但我无法正常工作。似乎尝试不起作用。

我的 auth.php

'driver' => 'eloquent',
        'model' => 'User',
        'table' => 'users',
            'reminder' => array(
               'email' => 'emails.auth.reminder', 'table' => 'password_reminders',
            ),

用户控制器.php

我已经读到我们需要用Hash::make();. 所以它已经完成了。

public function signin() 
    {

    $username = Input::get('username');
    $password = Input::get('password');

    $users = User::where('username', $username)->get();

    try {
        if (!sizeof($users))
            throw new Exception('Oops! Username or password don't match.');

        $user = $users[0];

        if (!$user)
            throw new Exception('Oops! Username or password don't match');

        if(Hash::check($password, $user->password))
            throw new Exception(Oops! Username or password don't match);

        Auth::attempt(array('name'=>$user->name, 'email'=>$user->email, 'password'=>$user->password));

        if (Auth::check())
        {
                throw new Exception('Works fine');
        }

        $this->success = 'YES';

    } catch (Exception $e) {
        $this->message = $e->getMessage();
    }

    return Response::json(array('success' => $this->success, 'message' => $this->message));
}

登录.blade.php

  <body>

    <div class="container">


      <form class="form-signin" role="form" onsubmit="return false;">
      {{ Form::token(); }}
        <h2 class="form-signin-heading">Login</h2>
        <input id="username" type="text" class="form-control" placeholder="Username" required autofocus>
        <input id="password" type="password" class="form-control" placeholder="Senha" required>
        <label class="checkbox">
          <input type="checkbox" value="remember-me"> Manter conectado
        </label>
        <button id="btn-signin" class="btn btn-lg btn-primary btn-block" type="submit">Entrar</button>
      </form>

    </div>

  </body>

  <script src="//code.jquery.com/jquery.js"></script>
  <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.0.1-p7/js/bootstrap.min.js"></script>

  <script type="text/javascript">

jQuery(document).ready(function($) {

$("#btn-signin").click(function() {     

    $.ajax({
            url: "/intranet/public/user/signin",
            type: "POST",
            data: {username: $("#username")[0].value, password: $("#password")[0].value},
            context: document.body, 
            success: function(json, status, xhr) {

                var message;

                if (json.success=="YES") {
                    window.location = "{{ url('/home')}}";
                } else {
                    alert(json.message);
                }
            }, 
            error: function(evt) {
                var response = jQuery.parseJSON(evt.responseText);
                alert(response.error.type+': '+response.error.message)
            }
        });
    });
});         
4

2 回答 2

3

我认为你做的太多了,我可以看到一些问题,在我看来这已经足够了:

public function signin() 
{
    $username = Input::get('username');
    $password = Input::get('password');

    $user = User::where('username', $username)->first();

    $this->message = '';

    try {

        if ( ! $user || ! Auth::attempt(array('email' => $user->email, 'password' => $password)) )
        {
            throw new Exception("Oops! Username or password don't match");
        }

        $this->success = 'YES';

    } catch (Exception $e) {
        $this->message = $e->getMessage();

        $this->success = 'NO';
    }

    return Response::json(array('success' => $this->success, 'message' => $this->message));
}
于 2013-12-13T14:47:51.717 回答
0
if(Hash::check($password, $user->password))

应该

if( ! Hash::check($password, $user->password))
于 2013-12-13T14:41:22.277 回答