2017-05-16 6 views
1

ユーザーのために再アクティブ化オプションを作成したいという問題がありますが、何度か試してもうまくいかず、混乱します。Laravelで 'Reactivation'オプションを作成する

public function handle($request, Closure $next) 
{ 
    if (!Auth::check()) { 
     if ($request->ajax()) { 
      return response('Unauthorized.', 401); 
     } else { 
      return redirect()->guest('/'); 
     } 
    } 
    else 
    { 
     $user = Auth::user();    
     if (!$user->activated) { 
      $activation = action('Auth\[email protected]', ['username' => $user->username]); 
      Auth::logout(); 

      return redirect()->guest('auth') 
        ->withErrors(array('message' => 'Please activate your account. Re-send your activation by clicking <a href=' . $activation . '>here</a>.')); 
     }   
     else if (!$user->enabled) { 
      Auth::logout(); 

      return redirect('/auth')->withErrors(array('message' => 'Your account has been deactivated. Please email ... for any inquiries.'))->withInput(); 
      // I tried to add the same method as the above if statement but not working here 
     } 

     $user->runDailyNotifications(); 
    } 
    return $next($request); 
}  

私はこの方法を使用して、私のデータベースを更新したかった:正常に動作しなければならない

$user = Auth::user(); 
    $user->enabled = 1; 
    $user->save(); 

ここ

は、ミドルウェア(オリジナルバージョン)です。

私はLaravelを初めて利用しています。最初は、これらのコードをミドルウェアに追加しました(これは間違いです)。

ちょっと試した後、ユーザーがログインを2回クリックするとアカウントを無効にした後にログインすることはできません。今どこからでもエラーメッセージを受け取ってから、どのように達成できるのだろうかと思っています。お手伝いありがとう!

答えて

0

私は以前のプロジェクトの1つで電子メールの確認と再確認を行っています。私はLoginControllerのログイン後のチェックで電子メール確認の検証を行った。あなたを助けるかもしれないいくつかのスニペットを投稿しましょう。

// Overwrite the authenticated method in LoginController 
protected function authenticated(Request $request, $user) 
{ 
    if ($user->isBanned()) { 
     $this->logout($request); 

     flashError('Your account has been banned.'); 

     return back(); 
    } 

    if (!$user->isEmailConfirmed()) { 

     $this->logout($request); 

     flashWarning('Email confirmation pending. <a href="'. route('confirm.resend', $user->id) .'">Click here</a> to resend confirmation email.'); 

     return back(); 
    } 

    return redirect()->route($this->redirectRoute); 
} 

public function resendConfirmationEmail(Request $request, User $user) 
{ 
    //resend confirmation email 
} 

public function confirmEmail(Request $request, $token) 
{ 
    // Validate the token and update the user email confirmation status 
} 

モデル

public function isBanned() 
{ 
    return (bool) $this->banned; 
} 

public function isEmailConfirmed() 
{ 
    return (bool) $this->confirmed; 
} 

ルート

Route::get('confirm/resend/{user}', 'Auth\[email protected]')->name('confirm.resend'); 
Route::get('confirm/email/{token}', 'Auth\[email protected]')->name('confirm.email'); 
関連する問題