3

当我的用户在我的应用程序中注册时,它会自动将他们重定向到/dashboard技术上很好的位置,但它不会检查confirmed数据库中的列是否具有1or的值0,它只是根据用户名和密码登录。

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

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

感谢您提供任何信息,

安迪

4

1 回答 1

3

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

我的路线.php:

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

    return "This is just an example";

}]);

我的内核.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);
    }
}

我试图为你尽可能地减少这个。

于 2016-02-08T21:20:35.780 回答