2014-12-05 28 views
0

enter image description here如何在Laravel 4中創建登錄功能?

我有3種用戶類型:

  1. 聯繫
  2. 分銷
  3. 內部

我有一個問題,在爲user type登錄。 (內部

我可以登錄,當我的用戶類型是管理員。 我可以登錄,當我的用戶類型是經銷商。

但我無法登錄,當我的用戶類型是內部的。有線 ????

我在我的AccountController.php的登錄函數中查看每行代碼。 我沒有注意到那裏有任何bug。如果你們發現有任何錯誤 - 請讓我知道。

這對我來說將是一個巨大的幫助。

這裏是我登錄的功能

GET

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

POST

public function postSignIn() { 
    $validator = Validator::make(Input::only('username','password'), 
     array(
      'username'  =>'required', 
      'password'  =>'required' 
      ) 
     ); 
    if ($validator->fails()) { 
     return Redirect::route('account-sign-in-post') 
     ->with('error','Username/Password Wrong or account has not been activated !') 
     ->withErrors($validator); 
    } 

     // Remember Me 
    $remember = (Input::has('remember')) ? true : false ; 
    $auth = Auth::attempt(array(
     'username' => strtolower(Input::get('username')), 
     'password' => Input::get('password'), 
     'active' => 1), 
    $remember); 


    // Keep track on log-in information of the user. 
    if(Auth::check()){ 
     $user = Auth::user(); 
     $user->last_logged_in = Input::get('created_at'); 
     $user->logged_in_count = $user->logged_in_count + 1 ; 
     $user->is_online = '1'; 
     $user->save(); 
    } 



    if($auth) { 
     return Redirect::to('/') 
     ->with('success','You have been successfully logged in.') 
     ; 
    } 
    else { 
     return Redirect::route('account-sign-in') 
     ->with('error','Username/Password Wrong or account has not been activated !') 
     ->withInput(Input::except('password')) 
     ->withErrors($validator); 
    } 

} 

VIEW

@extends ('layouts.form') 

@section('content') 

<style type="text/css"> 
    .signin{ 
     text-align: center; 
    } 
    #forgetpassword{ 
     /*color:#5cb85c;*/ 
     color:silver; 

    } 
</style> 

<form class="form-horizontal" role="form" action=" {{ URL::route('account-sign-in-post')}}" method="post" > 


@if ($errors->has('username')){{ $errors->first('username')}} @endif 
<div class="form-group"> 
    <label for=""> Email </label> 
    <input placeholder="Email" type="text" class="form-control" required name="username" {{ (Input::old('username')) ? 'value="'.e(Input::old('username')).'"' : '' }}> 
</div><br> 



@if ($errors->has('password')){{ $errors->first('password')}} @endif 
<div class="form-group"> 
    <label for=""> Password </label> 
    <input placeholder="Password" type="password" class="form-control" required name="password"> 
</div><br> 


<br> 


<button type="submit" class="btn btn-success btn-sm btn-block ">Sign In </button> 


    {{ Form::token() }} 


</form><br> 


<div class="col-lg-12 text-center"> 

    <a id="forgetpassword" href="{{ URL::route('account-forgot-password') }}"> Forget Password </a> <br> 


</div> 



@stop 

我確定我輸入了正確的用戶名和密碼,因爲我仔細檢查了我的數據庫。

它一直將我重定向到我的登錄頁面。 與('錯誤','用戶名/密碼錯誤或帳戶未被激活!')

有人可以告訴我,如果我做了什麼,我不是假設?

回答

1

在你的情況,你應該檢查你的Sign_In函數中的auth變量。

根據你的代碼,

$auth = Auth::attempt(array(
      'username' => strtolower(Input::get('username')), 
      'password' => Input::get('password'), 
      'active' => 1), 
     $remember); 

請記住,這些都是需要確保

  • username必須數據庫的數據庫匹配
  • password必須匹配
  • 用戶active必須是1

如果其中任何一項失敗,那麼您的登錄名爲STOP

  • 因爲您對用戶名和密碼很肯定,user active
  • 你檢查過它是否是1?

If Not

您的設置密碼功能或任何,您通常設置你的用戶有效。

只是這樣做:

$user->active = '1'; 
$user->save(); 

讓我知道如果這個工作!