Laravel 5.1 - 注册用户App重定向到/ dashboard但不检查帐户是否已确认

时间:2016-02-08 21:10:43

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

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

我很乐意提供代码,但现在我实际上并不知道你们需要查看的代码。

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

感谢任何信息,

安迪

1 个答案:

答案 0 :(得分:3)

我通过利用中间件来实现这一目标:

我的routes.php:

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

    return "This is just an example";

}]);

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

我试图尽可能地减少你。