<?php

namespace Buckhill\LaravelSAML\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Buckhill\LaravelSAML\Facade\AuthenticatedUser;
use Buckhill\LaravelSAML\Exceptions\NotAuthenticatedException;

class Authenticated
{
    /**
     * @param Request $request
     * @param Closure $next
     * @return mixed
     * @throws NotAuthenticatedException
     */
    public function handle($request, Closure $next)
    {
        if(!AuthenticatedUser::isAuthenticated())
        {
            return redirect('/sso/authenticate?RelayState=' . urlencode($request->url()));
        }

        return $next($request);
    }
}
