2014-02-16 77 views
0

我正在挖掘Laravel,看起来我的身份验证系统存在一些问题。我将尝试在下面编写我的代码片段。如果我的解释不够,请让我知道。auth :: attempt()总是返回false

路线:

/* 
Sign in (POST) 
*/ 
Route::post('/account/sign-in', array(
     'as' => 'account-sign-in-post', 
     'uses' => '[email protected]' 
)); 

/* 
Sign in (GET) 
*/ 
Route::get('/account/sign-in', array(
    'as' => 'account-sign-in', 
    'uses' => '[email protected]' 
)); 

AccountController.php

<?php 

class AccountController extends BaseController { 

public function getSignIn() { 
     return View::make('account.signin'); 
} 

public function postSignIn() { 
     $validator = Validator::make(Input::all(), 
      array(
       'email'  => 'required|email', 
       'password' => 'required' 
       ) 
      ); 

     if($validator->fails()) { 
      //Redirect to sign in page 
      return Redirect::route('account-sign-in') 
      ->withErrors($validator) 
      ->withInput(); 
     } else { 
      //Atempt user sign in 

      $auth = array(
       'email' => Input::get('email'), 
       'password' => Input::get('password'), 
       'active' => 1 
       ); 


      if(Auth::attempt($auth)) { 
       //Redirect to intended page 
       return Redirect::intended('/'); 
      } 
      else { 



       return Redirect::route('account-sign-in') 
        ->with('global', 'Email/password wrong, or       account not activated'); 


      } 
     } 

     return Redirect::route('account-sign-in') 
     ->with('global', 'There is a problem signing you in'); 
} 

public function getCreate(){ 
    return View::make('account.create'); 
} 

public function postCreate(){ 
    $validator = Validator::make(Input::all(), 
     array(
      'email'   => 'required|max:50|email|unique:users', 
      'username'  => 'required|max:20|min:3|unique:users', 
      'password'  => 'required|min:6', 
      'password_again'=> 'required|same:password' 
      ) 
     ); 

    if($validator->fails()) 
    { 
     return Redirect::route('account-create') 
     ->withErrors($validator) 
     ->withInput(); 
    } 
    else 
    { 
     $email  = Input::get('email'); 
     $username = Input::get('username'); 
     $password = Input::get('password'); 

     // Activation code 
     $code  = str_random(10); 

     $user = User::create(array(
       'email'  => $email, 
       'username' => $username, 
       'password' => Hash::make($password), 
       'code'  => (string)$code, 
       'active' => 0 
      )); 


    } 
} 


    return Redirect::route('home') 
    ->with('global','Account could not be activated. Please, try again later.'); 
} 
    } 

?> 

auth.php

<?php 
    return array(
    'driver' => 'eloquent', 
    'model' => 'User', 
    'table' => 'users', 
    'reminder' => array(
     'email' => 'emails.auth.reminder', 
     'table' => 'password_reminders', 
     'expire' => 60, 
    ), 
); 
?> 

user.php的

<?php 

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

    class User extends Eloquent implements UserInterface, RemindableInterface { 

protected $fillable = array('email' , 'username' , 'password', 'code'); 
/** 
* The database table used by the model. 
* 
* @var string 
*/ 
protected $table = 'users'; 

/** 
* The attributes excluded from the model's JSON form. 
* 
* @var array 
*/ 
protected $hidden = array('password'); 

/** 
* Get the unique identifier for the user. 
* 
* @return mixed 
*/ 
public function getAuthIdentifier() 
{ 
    return $this->getKey(); 
} 

/** 
* Get the password for the user. 
* 
* @return string 
*/ 
public function getAuthPassword() 
{ 
    return $this->password; 
} 

/** 
* Get the e-mail address where password reminders are sent. 
* 
* @return string 
*/ 
public function getReminderEmail() 
{ 
    return $this->email; 
} 

    } 

signin.blade.php

 @extends('layout.main') 

     @section('content') 
<form action="{{ URL::route('account-sign-in-post') }}" method="post"> 

    <div class "field"> 
     Email: <input type="text" name="email"{{ (Input::old('email')) ? ' value="' . Input::old('email') . '"' : ''}}> 
     @if($errors->has('email')) 
      {{ $errors->first('email') }} 
     @endif 
    </div> 

    <div class "field"> 
     Password: <input type="text" name="password"> 
     @if($errors->has('password')) 
     {{ $errors->first('password') }} 
     @endif 
    </div> 

    <input type="submit" value = "Sign in"> 
    {{ Form::token() }} 


</form> 
    @stop 

结论:1散列密码,它存储在哈希数据库。我正确使用function Auth::attempt()而不重新密码。我见过使用Auth::attemptHash::make($password)的人。 auth.php和User.php文件似乎很好。我不知道问题出在哪里。

+0

您是否通过将'active'设置为'1'来激活用户? – WebNovice

+0

该帐户已激活,列激活设置为1. –

回答

1

数据库中密码字段的长度必须是60或更高。

+0

就是这样!我的数据库中的密码列是varchar(50)。我一直在寻找我的代码。所以记住孩子们,数据库中的密码字段必须是60或更高。 –

相关问题