0

I am having issue and I'm trying to figure out what causes this.

SITE A

SITE A Code

web.php

Route::get('/eon/auth/check/login', 
    [ApiController::class, 'checkLogin']
    );
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\MemoModel;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;

class ApiController extends Controller
{
 
    public function checkLogin(Request $request){
        
        $username = 'admin';
        $token = 'xadasd12324';
        
            
            $user = User::where('username', $username)->first();


            return response()->json([
                'username' => $username,
                'token' => $token,
                'user' => $user->first_name
                // 'ok' => $ok
            ]);
    
            if($oauth_user){
               
                RateLimiter::clear($this->throttleKey($username));
                return response()->json([
                    'status_code' => 200,
                    'message' => 'Success',
                ]);
            }else{
                RateLimiter::hit($this->throttleKey($username), $seconds = 3600);              
    
                return response()->json([
                    'status_code' => 401,
                    'message' => 'Unauthorized2',
                ]);
            }
     
    }

    private function throttleKey($username)
    {
             
        return Str::lower(request('username'));
    }
    
    private function checkTooManyFailedAttempts($username)
    {
        if (! RateLimiter::tooManyAttempts($this->throttleKey($username), 50)) {
            return;
        }
   
        abort(403, 'IP address banned. Too many login attempts.');
    }
}

SITE B Code

web.php

Route::get('/eon/oauth/', [ApiController::class, 'oAuth']);
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;

class ApiController extends Controller
{
    public function oAuth(Request $request){
        
        $username = $request->u;
        $token = $request->id;

       dd($this->checkAuthToken($token, $username));
          
    }
    private function checkAuthToken($token, $username){
        

        $result = Http::get('http://site-a.local:8080/one/auth/check/login');
        $data = json_decode($result->body(), true);
        return $data;

        
    }
   
}

SITE A Result (calling the api via browser)

enter image description here

SITE B Result

enter image description here

I am trying to display the data from SITE A and do a conditional IF ELSE based on the data I retrieved from SITE A using the SITE B Controller.

I'm not very familiar with how Laravel API works or APIs in general, I tried Guzzle but I am still getting the same error, I guess it's something to do with Laravel restriction or structure with which I am not familiar.

7
  • Are both of these Laravel sites hosted on the same machine? Commented Jan 13 at 1:13
  • Check the site URL name is corrected. also add functionality to try & catch exception handling for the actual error. Commented Jan 13 at 6:22
  • Hello @Joundill Yes, they are both hosted in laravel and I am testing it on my local machine using the same web server Commented Jan 13 at 8:54
  • @DeveloperNilesh I tried and still I got the same display which is NULL Commented Jan 13 at 8:55
  • if you check with try catch and print the exception, we can track the error. Commented Jan 14 at 12:02

1 Answer 1

0

The $result->body() will return string not JSON data.

you should try as following.

try {
$url = 'http://site-a.local:8080/one/auth/check/login';

// a small timeout
$headResponse = Http::timeout(10)->head($url);

// check if url has response or not
 if (!$headResponse->ok()) {
   return response()->json(['message' => 'URL is not accessible or does not exist.'], 404);
  }

// get the response with time 
$response = Http::timeout(30)->get($url);

// if response is ok
if (!$response->ok()) {
  return response()->json(['message' => 'Failed to access URL: '], 400);
 }

// this contain all json data and you can access like $response['user']
return $response;
 
} catch (\Exception $e) {

  return response()->json(['message' => 'Error accessing URL: ', 'error' => $e->getMessage()], 500);

}
Sign up to request clarification or add additional context in comments.

8 Comments

Hello, I tried your code and it showing an error "message":"URL is not accessible or does not exist." see this screenshot: i.imgur.com/dTrx1lc.png - i think it still reading the routes and information from SITE B, I dont think its getting the information from SITE A - what do you think?
check the ResponseHeaderBag array what's there also check with another url or even some external valid URL like google.com. make sure no middleware is blocking the access.
Here's the draft or basic diagram of the process i.imgur.com/oiEvtZB.png I am not sure if what Im doing is a standard process.
could you edit your question and provide code for both routes I think you have problem in your routing and dynamic query param.
Here's what's inside the headerbag i.imgur.com/WLT9D0I.png
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.