2016-07-04 9 views
1

私のAuthControllerがほとんど条件をチェックしないようにして、それに従って適切なルートにリダイレクトするようにします。私は、ログインしているユーザーのDBに特定の列が空でないかどうかを確認したい。空ではない場合はホームページにリダイレクトしていますが、別のルートにリダイレクトしています。私はpostLoginメソッドを使用しました。ここでの問題は、条件を一切チェックせず、条件が満たされなくても家に直接リダイレクトすることです。私はDBの詳細が満たされていない新しいユーザーでログインしようとした後、ホームページにリダイレクトされ、DBに個人情報が入力されているユーザーにも同じように起こった。postLogin()Laravel 5.2でのログイン後のメソッドのリダイレクト

ここはAuthControllerのコード

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class AuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    /** 
    * Where to redirect users after login/registration. 
    * 
    * @var string 
    */ 
    //protected $redirectTo = '/user-prof'; 


    public function postLogin(Request $request) 
    { 


     $email = $request->email; 
     $user = \Auth::user(); 

     if(($user->username && $user->phone && $user->bio && $user->dobday && $user->dobmonth && $user->dobyear && $user->firstname 
      && $user->lastname && $user->topics && $user->nationality)!= NULL) 
     { 
      redirect('home'); 
     } 

     else 
     { 
      redirect('/user-prof'); 
     } 

     return $this->login($request); 
    } 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware($this->guestMiddleware(), ['except' => 'logout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|min:6|confirmed', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'name' => $data['name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 
    } 
} 

また、私はpostLogin()メソッド内のIF条件に正しい方法でDB列のデータをチェックしていますかどうかを知りたいです。

+0

これらのアトリビュートをチェックし、条件内で呼び出すモデルで関数を作成します。 –

+0

あなたはリダイレクトを 'リターン 'していないので、リダイレクトレスポンスを作成しているだけで何もしません。あなたのpostLoginルートが現在の認証ユーザーを取得しようとしている理由はわかりませんが、現時点ではないはずです。私は 'handleUserWasAuthenticated'によって成功した認証の後に呼び出される' authenticated'をチェックインしなければならないものだと感じています – lagbox

答えて

0

Authcontrollerのメソッドをオーバーライドしないでください。 postLoginルート上でミドルウェアを使用して、条件と経路を確認します。

https://laravel.com/docs/5.2/middleware

それを行うための一つの他の方法があります。しかし、私はあなたがそれを行うことを示唆しません。 RedirectsUsers特性を変更することができます。これはLaravelファイルであり、すべての作曲家の更新時に元に戻されます。あなたがリダイレクトに

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Foundation/Auth/RedirectsUsers.php

+0

これらのメソッドをオーバーライドするのは問題ありません。コントロールはあなたのアプリケーション空間にあり、あなたはそれがあなたが望むように何とか働くことができます – lagbox

+0

もちろんです。私はその質問の中でコード全体を読むのにはちょっと余裕がなく、私の心の中で最初に浮かんだものに答えました。私はあなたの答えが教育的であると感じます。 +1 – Ravi

2

を理解するためにそれを見ることができますが、ユーザが「認証済み」として記録された後は、おそらくこれらのチェックを行うことを意味します。あなたのAuthControllerその上authenticatedメソッドを定義することができますユーザーが認証された後にhandleUserWasAuthenticatedによって呼び出されます。

これはおそらくこのようなものです。 (ユーザーの属性を取得し、確認したいものだけを取得し、ヌルを削除するためにarray_filterを使用して、必要なフィールドがすべて存在するかどうかを確認してください)。

protected function authenticated($request, $user) 
{ 
    $required = [ 
     'username', 'phone', 'bio', 'dobday', 'dobmonth', 'dobyear', 
     'firstname', 'lastname', 'topics', 'nationality' 
    ]; 

    $userAtts = array_filter(array_only($user->toArray(), $required)); 

    if (count($userAtts) != count($required)) { 
     return redirect('/user-prof'); 
    } 

    return redirect('home'); 
} 

これはAuthControllerはすでに特性を経由している任意の方法を変更しませんが、それはちょうどので、それを呼び出す場合handleUserWasAuthenticatedが存在するかどうかを確認し、するメソッドを追加します。

Illuminate\Foundation\Auth\[email protected] -> @handleUserWasAuthenticated

これらのフィールドが埋められるまで、あなたは「ケージ」ユーザに予定がある場合は、その後、私はラヴィの答え@言っているものと同様のミドルウェアオプションを指定して行くだろうが、それに適用しなければならないであろう多くのルート。

関連する問題