vlab-research / vlab

The Virtual Lab platform!
https://studies.vlab.digital
Other
3 stars 2 forks source link

Feat: Allow Users to connect their facebook accounts #146

Open Spazzy757 opened 1 year ago

Spazzy757 commented 1 year ago

Situation

We want to allow users to connect their Facebook accounts from the dashboard, this will allow us to be able to get access to their Facebook Ad accounts without having to generate tokens themselves.

The Following steps need to be thought of:

https://graph.facebook.com/v16.0/oauth/access_token?fb_exchange_token=OLD_LONG_LIVED_TOKEN&client_secret=VLABS_FACEBOOK_APP_CLIENT_SECRET&client_id=VLABS_FACEBOOK_APP_CLIENT_ID&grant_type=fb_exchange_token

Which will return a new long lived token

{
    "access_token": "$NEW_TOKEN",
    "token_type": "bearer",
    "expires_in":  5182893 //around 60 days in seconds
}

NOTES: If the person who gave permissions for the token has changed their password it will immediately invalidate our token so we should warn them about this. We should also have functionality in our crons that will test these tokens in order to pick up invalid tokens as quick as possible

Spazzy757 commented 1 year ago

@nandanrao one proposal I have is extending the credentials data structure to have a status field like so:

CREATE TABLE IF NOT EXISTS credentials(
       user_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
       entity VARCHAR NOT NULL,
       key VARCHAR NOT NULL
       /* We allow it to be nullable to start */
       status VARCHAR,
       created TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
       details JSONB NOT NULL
);

This way we can start using the credentials models and write a validator that checks if the credentials are working. We can also use this in the UI to help people debug better in the future

Spazzy757 commented 1 year ago

We would need to finalize these two PR's:

As we would need the idea of a "Connected Account" to store this info (i.e Facebook just becomes another connected account)

Spazzy757 commented 1 year ago

We have an endpoint that can generate the token:

We now just need to do the frontend work