2012-11-05 45 views
2

您好我是新來laravel,我想我的登錄表單的代碼功能這裏是代碼:

我這是怎麼創建新的用戶(作品井)

public function action_newuser() 
{ 

    $email = Input::get('email'); 
    $password = Input::get('password'); 
    $new_user = Input::get('new_user', 'off'); 
    $first_name= Input::get('firstname'); 
    $last_name= Input::get('last_name'); 
    $username= Input::get('username'); 

    $user = new User(); 
    $user->email = $email; 
    $user->password = Hash::make($password); 
    $user->first_name =$first_name; 
    $user->last_name= $last_name; 
    $user->username= $username; 
    try{ 

     $user->save(); 
    } 
    catch(Exception $e) { 
     echo "Failed to create new user!"; 
    } 

這是我的登錄功能:

public function action_login() 
{ 

    $password = Input::get('password');  
    $username= Input::get('username'); 
    $remember= Input::get('remember', 'off'); 
    if($remember =='off') $remember=FALSE; 
    if($remember =='on') $remember=TRUE; 
    $credentials = array(
      'username' => $username, 
      'password' => $password, 
      'remember' => $remember 
     ); 
    if(Auth::attempt($credentials)) 
     { 
      return Redirect::to('dashboard/index'); 
     } 
     else 
     { 
      #I put this line to check are the values passed from my form are correct. 
      echo $password.' ' .$username.' '. $remember; 

     } 


} 

當我提交的登錄表單,總是顯示空白頁面有$的密碼,用戶名$值$和記值。這意味着Auth :: attempt()運行不正常。

可能是什麼問題?

+0

是'$記「真的是憑證嗎? –

+0

@WaleedKhan沒有問題。 Auth :: attempt將數組作爲參數('username','password','remember') – Hasan

+0

@HananAyan不是真的,內建的記憶是嘗試的第二個參數(數組$ remember,boolean $ remember)。第一個數組中的其他參數是您嘗試匹配的用戶列,如在where子句中。 http://laravel.com/docs/security#authenticating-users – brazorf

回答

3

傻瓜我! 儘管我已經多次檢查過,但在auth配置文件中看不到'username' => 'email',行。

它應該是'username' => 'username',因爲我要使用用戶名登錄。

+2

良好的觀察。這種錯誤往往不被人注意。在我身上發生過很多次。 – Aniket

3

檢查以確保您的數據庫密碼字段爲60個字符。

+0

其已有60個字符。我使用Hash :: check()檢查了密碼和hashed_pa​​ssword,它返回true。沒有問題。 – Hasan

+2

+1,這是我的錯。 – ULazdins

0

對於所有正在面臨Auth :: attempt()登錄失敗且具有有效信用的laravel 4開發人員!

解決方案:

  • 在應用程序/配置/ app.php變化類型: '密碼'=> MCRYPT_RIJNDAEL_128爲 '密碼'=> MCRYPT_RIJNDAEL_256並重新散列所有密碼
  • 在模型添加方法:

    public function setPasswordAttribute($pass) { 
    
        $this->attributes['password'] = Hash::make($pass); 
    } 
    
  • 在UserController中:

    //創建用戶或註冊

    public function postCreate() { 
    
        $input = Input::all(); 
        $user = new User; 
        $user->username = Input::get('username'); 
        $user->email = Input::get('email'); 
        $user->password = Hash::make(Input::get('password'));  
        $user = User::create($input); 
        return Redirect::action("Frontend\[email protected]"); 
    

    }

    //登錄或者註冊在

    public function postSignin() { 
    
        $userdata = array(
         'username' => Input::get('username'), 
         'password' => Input::get('password') 
        ); 
    
        if (Auth::attempt($userdata, true)) { 
         Session::put('username', Auth::user()->username); 
         return Redirect::action("Frontend\[email protected]"); 
        } else { 
         return Redirect::action("Frontend\[email protected]") 
           ->with('message', 'Your username/password combination was incorrect') 
           ->withInput(); 
    } 
    

希望我幫助別人那裏