php - Laravel 5.3 在登录时重定向被禁止的用户

标签 php laravel

您好,我正在尝试重定向任何尝试登录且具有 ban_status 的用户! = 0

我想知道对我来说最好的方法是什么,我想只是将它们注销并将它们重定向到禁止的页面,但我只想禁止用户访问此页面,或者在表单上添加一个错误来说明“你被禁止了”等。这是我当前的代码:

公共(public)函数登录(请求$request) { $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

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

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        if ($this->guard()->user()->ban_status === 0) { // Check if the user is banned or not before allowing login
            return $this->sendLoginResponse($request);
        }
        else {

            $this->guard()->logout();

            $request->session()->flush();

            $request->session()->regenerate();

            return redirect('/banned'); // If the user banned_status is set to 1, then they will be redirected to a banned page.
        }
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if (! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }

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

最佳答案

如果用户知道您的路线名称,该方法不会阻止用户直接访问您的其他路线。

您可以通过添加中间件来阻止这种情况,保护所有其他路由,如果中间件无法验证,则将用户重定向到被禁止的页面。

请参见此处:https://laravel.com/docs/5.3/middleware

关于php - Laravel 5.3 在登录时重定向被禁止的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316758/

相关文章:

php - 使用 codeigniter 同时插入和更新

PHPUnit 似乎没有运行 Laravel 迁移

php - 部署到服务器后更新数据库时出错。拉拉维尔 5

sql - 删除 JSON_AGG(聚合函数)中的括号和引号

php - Paypal 成功 url 错误 laravel

php - JavaScript 重定向

php - 如何使用 table2 中的检查字段将数据插入 table1 (Codeigniter)

javascript - 通过 Ajax 发送请求时如何停止挂起页面

php - 限制在并发的时间段添加一行

php - 如何在 Laravel 6 及以上版本中安装 Whoops