代码之家  ›  专栏  ›  技术社区  ›  lewis4u

Laravel 5.3附加条件认证

  •  1
  • lewis4u  · 技术社区  · 8 年前

    <?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还是什么?

    因此,问题是如何为用户登录添加更多的条件

    'active' => 1
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Alexey Mezenin    8 年前

    你可以使用 LoginController 在5.3中。一种方法是重写 sendLoginResponse() 方法,并在其开头添加以下内容:

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

    如果用户不活动,这将注销用户,并将其重定向到根用户。这将起作用 active

        2
  •  1
  •   J.C. Gras    5 年前

    您可以指定 additional condition ,如LoginController中的“活动”=1:

    <?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');
            if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
                // Authentication passed...
                return redirect()->intended('dashboard');
            }
        }
    }