如何检查用户已通过Laravel 5确认


How to check user is confirmed with Laravel 5

我正在尝试使用开箱即用的Laravel身份验证。身份验证不是问题,但我想检查用户是否已确认其电子邮件地址。

我如何让Laravel检查表值confirmed的值是否为 1。

在配置/身份验证中.php我已经设置了'driver' => 'database'所以如果我正确理解文档,我可以进行手动身份验证,我想我可以检查用户是否已确认他的帐户。

Laravel在哪里检查匹配的用户名和密码?

如果您开箱即用地使用 Laravel Auth,您想看看为您设置的 AuthController。

您将看到,这使用特征身份验证和注册用户向控制器添加行为。

在该特征中,您会发现该方法postLogin .

您需要通过将自己的postLogin添加到AuthController来覆盖此方法。您可以为初学者复制并粘贴该方法。

现在去看看关于身份验证的Laravel文档。向下滚动到它谈到"使用条件对用户进行身份验证"的位置。

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1]))
{
    // The user is active, not suspended, and exists.
}

更改 postLogin 方法中的attempt()代码以包含条件,如示例中所示。在您的情况下,您可能希望传入条件 'confirmed' => 1 而不是活动状态,具体取决于您在 users 表中对字段的调用。

这应该让你开始!

创建一个中间件类:

<?php namespace App'Http'Middleware;
use Closure;
use Illuminate'Contracts'Auth'Guard;
class UserIsConfirmed {
    /**
     * Create the middleware.
     *
     * @param  'Illuminate'Contracts'Auth'Guard  $auth
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }
    /**
     * Handle an incoming request.
     *
     * @param  'Illuminate'Http'Request  $request
     * @param  'Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->user()->isConfirmed())
        {
            // User is confirmed
        }
        else
        {
            // User is not confirmed
        }
        return $next($request);
    }
}

我不知道在用户被确认或未得到确认的情况下你想做什么,所以我将把实现留给你。