php - Laravel 5.1 - 当注册用户应用程序重定向到/dashboard 但不检查帐户是否已确认

标签 php authentication laravel-5 laravel-5.1 laravel-authorization

当我的用户在我的应用程序中注册时,它会自动将他们重定向到 /dashboard,这在技术上是可行的,但它不会检查 confirmed 列是否在数据库的值为10,它只是根据用户名和密码登录。

我很乐意包含代码,但现在我实际上不知道你们需要看什么代码。

我需要它来检查 confirmed 列,如果它是 0,而不是让它们登录并抛出错误。

感谢您提供任何信息,

安迪

最佳答案

我通过使用中间件实现了这一点:

我的 routes.php:

Route::get('home', ['middleware' => 'auth', function ()    {

    return "This is just an example";

}]);

我的 Kernel.php:

protected $routeMiddleware = [

        'auth' => \App\Http\Middleware\Authenticate::class,

    ];

我的 Authenticate.php 中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    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->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }  
        }

        $user = $this->auth->user();
        if (!$user->confirmed) {
            $this->auth->logout();
            return redirect()->guest('auth/login')->with('error', 'Please confirm your e-mail address to continue.');
        }

        if (!$user->type) {
            $this->auth->logout();
            return redirect()->guest('auth/login')->with('error', 'A user configuration error has occurred. Please contact an administrator for assistance.');
        }    

        return $next($request);
    }
}

我试着为你尽可能地减少它。

关于php - Laravel 5.1 - 当注册用户应用程序重定向到/dashboard 但不检查帐户是否已确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279389/

相关文章:

php - MySql Insert 不起作用并且没有显示错误

security - 无需第三方工具即可在 Elasticsearch 中进行用户身份验证

laravel - 根据 Auth Laravel 5.5 绑定(bind)服务

css - Laravel 5 - 电子邮件中的内联 CSS

python - 在django中扩展用户模型后,我无法登录

php - Laravel Guzzle 请求获取错误的数据库连接

php添加数字

php - PDO +评论+? =错误‼

php - symfony/thanks 包含一个被你的 allow-plugins 配置阻止的 Composer 插件

asp.net - 检索客户端的 PC 名称? (Windows 身份验证)