<?php

namespace twa\ecomauth\middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
use twa\apilibs\traits\APITrait;

class AuthenticationRequiredCheck extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    use APITrait;
    protected $except = [
        //
    ];

    public function handle($request, Closure $next)
    {

        if (!request()->user || !request()->user->login || !property_exists(request()->user, 'column') || request()->user->column != "ecom_users_id" || !request()->user->login) {

            $url = url()->current();
            if($url) {
                Session::put('auth_redirect_url', $url);
                Session::put('disable_auth_redirect_overwrite', 1);
                Session::save();
            }

            return redirect(ecom('url')->prefix().'/login');
        }

        return $next($request);
    }
}
