Support the ongoing development of Laravel.io →
API Authentication Authorization
Last updated by @jackf007 4 weeks ago.
0

To troubleshoot the issue where the user_id is returning NULL, you can follow these steps:

  1. Ensure User Authentication: Make sure that the user is authenticated before reaching the createWebhookSubscription method. If Auth::id() is returning NULL, it typically means the user is not authenticated.

  2. Middleware: Ensure that your route is protected by authentication middleware. This will ensure that only authenticated users can access this route.

  3. Debug Logging: Add more debug logging to see where the issue might be.

  4. Check Token Validity: Ensure that the token used in Http::withToken($this->bearer) is valid and not expired.

Here's a revised version of your code with additional debug logging and checks:

Controller Method

public function createWebhookSubscription(Request $request) {
    $user_id = Auth::id();

    // Debug log for user authentication
    if (is_null($user_id)) {
        Log::error('User not authenticated or user ID is null', ['user_id' => $user_id]);
        return response()->json(['status' => 'error', 'message' => 'User not authenticated'], 401);
    }

    Log::info('Authenticated user', ['user_id' => $user_id]);

    $this->deleteAllWebhooks();

    $url = "https://api.calendly.com/webhook_subscriptions";
    $postData = [
        "url" => $this->urlWebhook,
        "events" => ["invitee.created", "invitee.canceled", "invitee_no_show.created", "invitee_no_show.deleted", "routing_form_submission.created"],
        "organization" => $this->requestCurrentOrganizationUser(),
        "scope" => "organization",
        "user_data" => [
            "user_id" => $user_id 
        ]
    ];

    Log::info('Request data for creating webhook subscription', ['data' => $postData]);

    $response = Http::withToken($this->bearer)
        ->withHeaders([
            'Content-Type' => 'application/json',
        ])
        ->post($url, $postData);

    if ($response->successful()) {
        Log::info('Webhook subscription created successfully', ['response' => $response->json()]);
        return response()->json(['status' => 'success', 'message' => 'Webhook subscription created successfully']);
    } else {
        Log::error('Failed to create webhook subscription', ['response' => $response->body()]);
        return response()->json(['status' => 'error', 'message' => 'Failed to create webhook subscription'], 500);
    }
}

Route

Make sure your route is protected by authentication middleware:

Route::middleware('auth')->post('/create-webhook-subscription', [CalendlyController::class, 'createWebhookSubscription']);

Middleware in Kernel

Ensure that your middleware is set up correctly in app/Http/Kernel.php. For example:

protected $routeMiddleware = [
    // Other middleware
    'auth' => \App\Http\Middleware\Authenticate::class,
];

Debugging

If the user is authenticated and the user_id is still NULL, it could be an issue with the authentication setup itself. Check the following:

  • Session Configuration: Ensure that your session configuration is correct.
  • Auth Guards: Verify that the correct authentication guard is being used.

Add a debug line at the beginning of your method to see all request data:

Log::info('Request received', ['request' => $request->all()]);

Testing

To test if the authentication is working, you can add a simple route and controller method to return the authenticated user's ID:

Route::middleware('auth')->get('/test-auth', function () {
    return response()->json(['user_id' => Auth::id()]);
});

Navigate to /test-auth while logged in and ensure it returns the correct user_id.

By following these steps, you should be able to identify why Auth::id() is returning NULL and resolve the issue.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

GiacoF jackf007 Joined 25 Jun 2024

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.