<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class tokencheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $secret = '0b2a5f5606cdoftnmltukfdbvvc19b107ad43rdf22';
        $header_secret = $request->header('secret');
        
        $lang = $request->header('lang') ?? 'ar';
        $availabLelocals = array_keys(config('laravellocalization.supportedLocales') ?? []);
        $lang = in_array($lang,$availabLelocals) ? $lang : LaravelLocalization::getCurrentLocale();
        LaravelLocalization::setLocale($lang);
       
        if($header_secret != $secret){
             return response()->json([
            'data'=>'connection error'
            ],  401);
        }
        return $next($request);
    }
}
