2017-08-21 14 views
0

laravel 5.4でユーザーを認証しようとしていますが、試行()を使用してユーザーを認証できますが、ユーザーがアクティブかどうか、ユーザーが非アクティブであるかどうかに応じてカスタムメッセージ、異なるメッセージ、およびユーザー名のパスワードが異なる場合は別のメッセージ。手動でLaravelでユーザーを認証するにはどうすればいいですか?

私の認証コード:

ここ
public function checklogin(Request $request) 
    { 
     $password = bcrypt($request->password); 
     $userData = User::where('email',$request->email)->where('password',$password)->first(); 
     if($userData!=NULL) 
     { 
      $credentials=array('email' => $request->input('email'),'password' => $request->input('password'),'status' => 'active'); 

      if(Auth::guard('users')->attempt($credentials)) 
       return redirect()->intended('/dashboard'); 
      else 
      { 
       Mail::to($request->email)->send(new VerifyUser($userData)); 
       return redirect('/')->with('error','Your account is unverified - check your email for account verification link'); 
      } 
     } 
     else 
      return redirect('/')->with('error','Invalid username or password'); 


    } 

、私が最初に彼らが無効である場合と仮定する場合、それは直接、エラーメッセージ「無効なユーザー名またはパスワード」と表示されます、ログイン資格情報が正しいかどうかをチェックしたいですログイン信用状が正しい場合、私はログインを試みようとしています。ユーザーが非アクティブな場合は、「あなたのアカウントを確認する必要があります」というメッセージを表示したい場合、ユーザーがアクティブであればユーザーをダッシュ​​ボードにリダイレクトします。

私の場合、私はパスワードをbcryptして、dbテーブルのパスワードで十字チェックしても私のユーザを認証できません。

+0

可能性の重複を... https://stackoverflow.com/questions/33390547/how-can-i-add-extra-condition-in-laravel-authentication、ドキュメントhttps://laravel.com/docs/5.1/authentication#authenticating-users –

+0

これは私の望むものではない、私のクエリはbcryptを使っている。私はテーブルに格納されたパスワードを照合することができない。これを行う方法は他にある? – dollar

+0

'$ request-> input( 'password')'を 'bcrypt'を使ってハッシュされたパスワードではない' credentials 'に渡しています。 –

答えて

1

しかし、これを行うには多くの方法がありますデータベースのハッシュされたパスワードと一致しない新しいハッシュ。

あなたが代わりにこれを行うことができます:

$userData = User::where('email',$request->email)->first(); 

if ($userData && Hash::check($request->password, $userData->password)) 
{ 
    // The passwords match... 
} 
1

Noticeコメント。これはうまくいくはずです。あなたはbcryptが生成するので動作しません上記

$userData = User::where('email',$request->email)->where('password',$password); 

ラインを動作するようにあなたのコードの行4を得ることに興味を持っていることあなたのコメントで述べたので、

public function checklogin(Request $request) 
    { 
     $password = bcrypt($request->password); 
     $userData = User::where('email',$request->email)->where('password',$password); 
     if($userData->get()->count() > 0) //Check if User exists 
     { 
     if($userData->where('status','inactive')->get()->count() > 0) // Check if User is inactive 
     { 
      Mail::to($request->email)->send(new VerifyUser($userData->get()->first())); 
      return redirect('/')->with('error','Your account is unverified - check your email for account verification link'); 
     } 

     $credentials=array('email' => $request->input('email'),'password' => $password); // passing hashed password 

     if(Auth::guard('users')->attempt($credentials)) 
      return redirect()->intended('/dashboard'); 
     } 
     else 
     return redirect('/')->with('error','Invalid username or password'); 
    } 
関連する問題