Reliable broadcasting? steemconnect-firebase-functions v2.0 is here!
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:
- Github: https://github.com/jakipatryk/steemconnect-firebase-functions
- SteemProjects: https://steemprojects.com/projects/p/steemconnect-firebase-functions/
- NPM: https://www.npmjs.com/package/steemconnect-firebase-functions
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
- https://github.com/jakipatryk/steemconnect-firebase-functions/pull/43
- https://github.com/jakipatryk/steemconnect-firebase-functions/pull/46
- https://github.com/jakipatryk/steemconnect-firebase-functions/pull/48
- https://github.com/jakipatryk/steemconnect-firebase-functions/pull/53
- https://github.com/jakipatryk/steemconnect-firebase-functions/pull/44
Posted on Utopian.io - Rewarding Open Source Contributors
Leave Reliable broadcasting? steemconnect-firebase-functions v2.0 is here! to:
Read more #utopian-io posts
Best Posts From Patrick What
We have not curated any of jakipatryk's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Patrick What
- Algorithmic approach to real world problems - rook polynomials
- Steeditor development update: unlimited content editing and dashboard
- Steeditor development update: templates, images upload, and more!
- Steeditor development update: communities, new core, and new editor
- Create a logo for Steeditor [+30 SBD reward]
- Steeditor - an advanced editor, not only for advanced users!
- steemconnect-firebase-function with a new documentation: why did I use MkDocs instead of JSDoc or Typedoc?
- Reliable broadcasting? steemconnect-firebase-functions v2.0 is here!
- Road to steemconnect-firebase-functions 2.0.0: what has been done so far?
- I could regularly STEAL 25% of the rewards from Utopian, but I did NOT do that