<?php

namespace App\Game\Services;

use App\Game\GlobalUserInfo;
use App\Http\Controllers\Game\LoginController;
use Closure;

class MustGameLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $user = GlobalUserInfo::$me;
        if (!$user) {

            $response=response()->json(apiReturnFail(['login.notfound', 'Sua conta não foi encontrada, registre-se ou tente novamente!'],"",404));
            if($request->hasCookie('guuid'))$response=$response->withCookie(LoginController::clearLoginCookie());
            return $response;
        }
        GlobalUserInfo::UpdateLoginDate($request);
        return $next($request);
    }
}
