2017-02-26 10 views
0

イム建物の認証プロセスlaravel 5.4デフォルトの認証を使用して、私の計画は私がログインする前「アクティブにする必要があり、ユーザーの電子メール」いくつかの追加の条件を確認したいですショーのエラーメッセージ認証ログイン、Laravel 5.4、ユーザーがいないアクティブ

私はこの

protected function attemptLogin(Request $request) 
{     
    $qLogin = [ 
     'user_email'  => $this->credentials($request)[$this->username()], 
     'password'   => $this->credentials($request)[$this->password()], 
     'active'   => 1 
    ];   


    return $this->guard()->attempt(
     $qLogin, $request->has('remember') 
    ); 
} 

コードが私の願いとして検証し、私は、ユーザーに伝えるいくつかのエラーメッセージを表示するように、彼らならば、彼/彼女は、アクティブ/自分の電子メールを確認する必要があり、私のLoginControllerにメソッドattempLoginを上書きしようとしていますログインするには

laravelのために新しいことを申し訳ありません。あなたの助けは素晴らしいでしょう。

+0

私は同じ問題があります。 –

答えて

0

ユーザーがアクティブになっているかどうかを確認するために、認証機能を追加できます。また、sendFailedLoginResponse関数を変更してエラーメッセージをカスタマイズすることもできます。

パス:アプリ\のHttp \コントローラ(私はアプリケーション\モデルに私のUserモデルを移動するので、ここでのApp \モデルが\ユーザー)\ LoginController

が点灯\のHttp \要求とApp \ユーザーをインポートすることを忘れないでください。

<?php 

namespace App\Http\Controllers\Auth; 

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 
use Illuminate\Http\Request; 
use App\Models\User; 


class LoginController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles authenticating users for the application and 
    | redirecting them to your home screen. The controller uses a trait 
    | to conveniently provide its functionality to your applications. 
    | 
    */ 

    use AuthenticatesUsers; 

    /** 
    * Where to redirect users after login. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/home'; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest')->except('logout'); 
    } 

    protected function credentials(Request $request) { 
     return array_merge($request->only($this->username(), 'password'), ['active' => 1]); 
    } 

    /** 
    * Get the failed login response instance. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\RedirectResponse 
    */ 
    protected function sendFailedLoginResponse(Request $request) 
    { 
     $errors = [$this->username() => trans('auth.failed')]; 

     // Load user from database 
     $user = User::where($this->username(), $request->{$this->username()})->first(); 

     // Check if user was successfully loaded, that the password matches 
     // and active is not 1. If so, override the default error message. 
     if ($user && \Hash::check($request->password, $user->password) && $user->active != 1) { 
      $errors = [$this->username() => trans('auth.noactive')]; 
     } 

     if ($request->expectsJson()) { 
      return response()->json($errors, 422); 
     } 
     return redirect()->back() 
      ->withInput($request->only($this->username(), 'remember')) 
      ->withErrors($errors); 
    } 
} 

はところで、私は、\ EN \ auth.php私は(「auth.notactivated」)、トランスを使用することができますので、私のエラーメッセージなどのリソース\ LANGの行を追加します。

<?php 

return [ 
    'failed' => 'These credentials do not match our records.', 
    'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', 
    'notactivated' => 'This account has not been activated yet.', 
]; 
関連する問題