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

How to check user is confirmed with Laravel 5

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

我如何让 Laravel 检查 table 值 confirmed 是否具有值 1。

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

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

如果您正在使用开箱即用的 Laravel Auth,您想要查看已为您设置的 AuthController

您会看到这使用特征 AuthenticatesAndRegistersUsers 向控制器添加行为。

在该特征中,您将找到方法 postLogin

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

现在去看看Laravel docs on authentication。向下滚动到它谈论 "Authenticating A User With Conditions."

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

更改 postLogin 方法中的 attempt() 代码以包含条件,如示例中所示。在您的情况下,您可能希望传递条件 'confirmed' => 1 而不是 active,具体取决于您在用户 table.

中对字段的称呼

这应该让你继续!

创建一个中间件class:

<?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);
    }

}

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