Events

# Available Events

Event
EVENT_USER_CONNECTED
EVENT_USER_DISCONNECTED
EVENT_WEBHOOK_SYNC

# Connected

To recieve data when a user connects their Strava account to Craft, use the EVENT_USER_CONNECTED event. This returns $event->user:

use bymayo\stravasync\events\UserConnectedEvent;
use bymayo\stravasync\services\UserService;
use yii\base\Event;

Event::on(
    UserService::class,
    UserService::EVENT_USER_CONNECTED,
    function(UserConnectedEvent $event) {
    // Do something
    }
);

# Disconnected

To recieve data when a user disconnects their Strava account to Craft, use the EVENT_USER_DISCONNECTED event. This returns $event->user:

use bymayo\stravasync\events\userDisconnectedEvent;
use bymayo\stravasync\services\UserService;
use yii\base\Event;

Event::on(
    UserService::class,
    UserService::EVENT_USER_DISCONNECTED,
    function(userDisconnectedEvent $event) {
    // Do something
    }
);

# Webhook

If you want to receive data from the Strava Webhook Events API (https://developers.strava.com/docs/webhooks/) when an activity/athlete is created or updated for example, you can use the plugins EVENT_WEBHOOK_SYNC event.

To initally set this up, you need to request Webhook access from Strava (See Webhooks Overview on https://developers.strava.com/docs/webhooks). Strava will then enable your account to access the Webhooks feature.

Next, you need to create a Webhook Subscription by doing a POST request to the Strava Sync webhook controller (http://website.com/strava-sync/webhook/sync) with a Bearer Token and client_id, client_secret, verify_token, callback_url parameters (The callback_url should be the same as the POST request URL)

This will then return a callback validation. If this is successful you will get back an id (It's worth making note of this to view/delete the subscription during your project developement)

Once the subscription has been created, you can now use the webhookSync event. So whenever an activity/athlete is created, edited or deleted on Strava you can get data back from it for your own plugin/module:

use bymayo\stravasync\events\WebhookSyncEvent;
use bymayo\stravasync\services\WebhookService;
use yii\base\Event;

Event::on(
    WebhookService::class,
    WebhookService::EVENT_WEBHOOK_SYNC,
    function(WebhookSyncEvent $event) {
    // Do something
    }
);

The $event returns an $event->athlete and $event->request property.

The $event->athlete property contains the userId, athleteId and accessToken of the validated Strava user. The $event->request property contains all Event data from the Strava Webhook e.g. object_type which is either athlete, or activity aswell as aspect_type which returns whether it's new, updated etc (See Event Data on https://developers.strava.com/docs/webhooks/)