2016-10-26 8 views
1

Laravel 5.3では何かが変更されました。Laravel 5.3追加条件による認証

<?php 

namespace App\Http\Controllers; 

use Illuminate\Support\Facades\Auth; 

class AuthController extends Controller 
{ 
    /** 
    * Handle an authentication attempt. 
    * 
    * @return Response 
    */ 
    public function authenticate() 
    { 
     if (Auth::attempt(['email' => $email, 'password' => $password])) { 
      // Authentication passed... 
      return redirect()->intended('dashboard'); 
     } 
    } 
} 

しかし、このコントローラ:ドキュメントで私たちは、追加の条件を追加することができ、このコントローラがあるAuthControllerはもう存在しません.....だけLoginController

私はAuthControllerことを作成するべきでは ありまたは何?

そこで質問は、あなたがそのために5.3でLoginControllerを使用することができます

'active' => 1 

答えて

2

にログインするユーザのために1つの以上の条件を追加する方法です。

if (!auth()->user()->active) { 
    auth()->logout(); 
    return redirect('/'); 
} 

これにより、ユーザは、彼がアクティブではありませんし、ルートに彼をリダイレクトするログアウトします:あなたが欲しいものを行うための1つの方法はsendLoginResponse()メソッドをオーバーライドして、それの初めにこのような何かを追加することです。これはactiveがブール値である場合に動作します。

+1

ニース...私はこのようにそれを作ったよ!を) - > user() - > active){ auth() - > logout(); リダイレクト( 'login') - > with( 'warning'、 'Sie sind noch nicht aktiviert!'); } } – lewis4u

+0

喜んで助けました。 –

+0

もう1つの質問ですが、今すぐ新しいユーザーを登録するとすぐにログインします。これを防ぐ方法は?それで私は新しい質問をするべきですか? – lewis4u

0

あなたはLoginControllerで、= 1 'アクティブ' のようなadditional condition、指定することができます(パブリック関数sendLoginResponse() { 場合(AUTH:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Support\Facades\Auth; 

class LoginController extends Controller 
{ 
    /** 
    * Handle an authentication attempt. 
    * 
    * @return Response 
    */ 
    public function authenticate(Request $request) 
    { 
     $email = $request->input('email'); 
     $email = $request->input('email'); 
     if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) { 
      // Authentication passed... 
      return redirect()->intended('dashboard'); 
     } 
    } 
} 
関連する問題