<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class RedirectIfAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            $user = Auth::user();
            if ($user->email_verified == 0) {
                return redirect()->route('verification.notice')->with('message', 'You need to verify your email address.');
            }

            if ($user->is_admin == 1) {
                return redirect('admin/dashboard');
            } else {
                return redirect('/dashboard');
            }
        }

        return $next($request);
    }
}
