<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        /*
        * if user is blocked then it will give error response in all apis
        */
        if(Auth::check() && Auth::user()->is_block == 1){
            Auth::user()->update([
                'is_active' => 0
            ]);
            Auth::logout();
            
            return response()->json([
                'status' => false,
                'status_code' => 401,
                'message' => "Your account is temporary suspended",
                'data' => array(),
                'errors' => array()
            ], 401);
        }
        
        return $request->expectsJson() ? null : route('unauthenticated');
    }
}
