Reliable broadcasting? steemconnect-firebase-functions v2.0 is here!

in #utopian-io7 years ago (edited)

I'm happy to finally introduce version 2.0 of steemconnect-firebase-funtions, which brings the library to a next level. I hope that changes described in this contribution and in my previous one will make development of apps built with Firebase and SteemConnect easier.

Quick recap

steemconnect-firebase-functions is a TypeScript library that can help you build applications with SteemConnect and Firebase. The library makes it easy to:

  • implement OAuth2 Authorization Code Grant
  • broadcast operations to the Steem blockchain
  • check OAuth2 error types
  • mint Firebase Custom Tokens
  • create Firebase Auth accounts for your users with additional data
  • store tokens in the Firebase Cloud Firestore

Links:

New documentation

For version 2.0, I've made new documentation. The new one is available here.

More on that in my next contribution.

New features

rely for reliable broadcasting

Most of the changes in my previous contribution were made to make the function rely possible to be built. For that, I introduced a few concepts. But what does rely do anyway?

SteemConnect's access_token expires 7 days after issuing. If your app didn't use server-side authentication, your user would have to refresh the access token himself by logging into SteemConnect once again.

But it is not the case when you use OAuth2 authorization code grant with offline scope. During this OAuth2 flow you get, along with access_token, another token called refresh_token, which doesn't expire and lets you refresh the access token.

How should the refresh_token be used?

In my opinion, if a request to the SteemConnect service fails cause of expired access_token, the refresh_token should be used to get a new access_token and the request should be repeated.

The rely function does exactly that .

First of all, it wrapps a broadcastable function, client credentials, and a AccessTokenResponse object (with properties: access_token, refresh_token, username, expires_in).

Next, it tries to broadcast the broadcastable with provided access_token.

If the request is successful, it simply returns the result of broadcast. If it's not, it checks if the error is caused by expired access_token and if so, it refreshes it using provided refresh_token.

The request is being sent once again, this time with new access_token, and if successful, the rely function returns the result of broadcast along with new tokens.

Example usage
import {
  rely,
  broadcastUpvote,
  Vote,
  AccessTokenResponse,
  ClientCredentials
} from 'steemconnect-firebase-functions';

const clientCredentials: ClientCredentials = {
  clientId: 'strimi.app',
  clientSecret: '432rnj3nr23nkvfdvdf'
};
const accessToken: AccessTokenResponse = {
  access_token: 'fdsfertre',
  expires_in: 4323432,
  username: 'jakipatryk',
  refresh_token: '3rk3m2krl3'
};
const voteOptions: Vote = {
  author: 'ned',
  permlink: 'i-am-ned',
  weight: 10000
};

const broadcastableUpvote = broadcastUpvote(voteOptions);

rely(clientCredentials)(accessToken)(broadcastableUpvote).then(response =>
  console.log(response)
);
//  {
//    result: { ... }
//    access_token: 'new access token if refreshed, otherwise undefined`
//    refresh_token: 'new refresh token if refreshed, otherwise undefined'
//    username: ...
//    expires: ...
//  }

For more, check the documentation and implementation.

Autocompleting author of operation

I changed the behavior of functions from broadcasting module.

As they require the entire AccessTokenResponse object as a parameter, so I decided to always autocomplete the author of operation with username property of AccessTokenResponse.

Example
import {
  broadcastFollow,
  Follow,
  AccessTokenResponse
} from 'steemconnect-firebase-functions';

const followConfig: Follow = {
  userToFollow: 'jakipatryk'
};
const accessToken: AccessTokenResponse = {
  access_token: 'kj3n4jn2342.432p4k2p',
  expires_in: 640000,
  username: 'ned'
};

broadcastFollow(followConfig)(accessToken).then(response =>
  console.log(response)
);
//  {
//    result: { ... }
//  }

AccessTokenResponse everywhere

I decided to require the entire AccessTokenResponse object for every function which needs either access_token or any other properties.

I think it limits confusion, before this change, accessToken sometimes referred to the access_token string, sometimes to the object AccessTokenResponse.

Scope type

For better developer experience, I've added a Scope type:

export type Scope =
  | 'login'
  | 'offline'
  | 'vote'
  | 'comment'
  | 'comment_options'
  | 'custom_json'
  | 'delete_comment'
  | 'claim_reward_balance';

delete_comment operation

Last but not least new feature in 2.0 is a support for delete_comment operation, which allows to delete a comment or post.

I've added the operation creator createDeleteComment, the broadcastable creator createBroadcastableDeleteComment, and broadcast function broadcastDeletion.

How to contribute?

Do you have a question?

Feel free to contact me on Discord (jakipatryk#1263) or on Steem.chat (jakipatryk).

Have you found a bug?

Publish a Utopian contribution and add an Issue to the project on Github!

Do you want to write some code?

Fork the repo, do your changes and create a pull request!

Pull requests



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thanks for the contribution, it has been approved.

Really great work, keep it up!


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

@jakipatryk, it's a great job you have done and an outstanding work for the benefit of steemians.
Thanks for your efforts and lots of more wisdom and luck.

Hey @jakipatryk! Thank you for the great work you've done!

We're already looking forward to your next contribution!

Fully Decentralized Rewards

We hope you will take the time to share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.me/utopian-io

Sir, your post is very beautiful, your posts are so beautiful I think we have a lot to learn from your posts

Loading...

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.035
BTC 92456.60
ETH 3136.76
USDT 1.00
SBD 3.21