Skip to content

OAuth2

What is this module for?

This module is a helper in the process of implementing the OAuth2 Authorization Code Grant flow with SteemConnect.

Functions available here make it easy to generate URL to SteemConnect with correct scope and other details, get tokens from SteemConnect, refresh and revoke an access token, and set and get user metadata.

API reference

Functions

getAuthorizationUrl

export declare function getAuthorizationUrl({
  clientId,
  redirectUri,
  scope,
  state
}: ClientCredentials & {
  redirectUri: string;
  scope: Array<Scope>;
  state?: string;
}): string;
Definition

Generates authorization URL to SteemConnect service.

Parameters
  • config (ClientCredentials +
    redirectUri (string): the URI you want your user to be redirected to after logging in to SteemConnect
    scope (Array<Scope>): the array of scopes you want to get access for your user to
    state? (string): optional state variable):
    configuration object for the app
Returns
  • (string): the authorization URL
Example Usage
import { getAuthorizationUrl, Scope } from 'steemconnect-firebase-functions';

const clientId: string = 'strimi.app';
const redirectUri: string = 'https://strimi.pl/redirect';
const scope: Array<Scope> = ['vote'];
const state: string = 'state342343243242';

const authorizationUrl = getAuthorizationUrl({
  clientId,
  redirectUri,
  scope,
  state
});
// 'https://steemconnect.com/oauth2/authorize?client_id=strimi.app&response_type=code&redirect_uri=https%3A%2F%2Fstrimi.pl%2Fredirect&scope=vote&state=state342343243242'
Implementation

The implementation is available on Github.


getAccessToken

export declare function getAccessToken({
  clientId,
  clientSecret,
  redirectUri,
  code
}: ClientCredentials & {
  redirectUri: string;
  code: string;
}): Promise<AccessTokenResponse>;
Definition

Gets access token for a user (access_token, expires_in, username, and optionally refresh_token) from SteemConnect.

Parameters
  • config (Required<ClientCredentials> +
    redirectUri (string): the redirect URI used earlier in the getAuthorizationUrl
    code (string): the code retrived from the SteemConnect):
    the configruation object
Returns
Example Usage
import { getAccessToken, ClientCredentials } from 'steemconnect-firebase-functions';

const clientCredentials: ClientCredentials = {
    clientId: 'strimi.app',
    clientSecret: 'gfd65464fdsfsoi3o'
};
const redirectUri = 'https://strimi.pl/redirect';
const code = '435435435afsdfdsfsd.3rkdmsfklmdlf';

getAccessToken({
  ...clientCredentials
  redirectUri,
  code
}).then(tokens => console.log(tokens));

//  {
//      access_token: '...',
//      expires_in: 640000,
//      username '...'
//      refresh_token: '...' // if scope included `offline`
//  }
Implementation

The implementation is available on Github.


refreshAccessToken

export declare function refreshAccessToken({
  clientId,
  clientSecret,
  refresh_token
}: Required<ClientCredentials> & Required<AccessTokenResponse>): Promise<
  AccessTokenResponse
>;
Definition

Uses refresh_token to get new AccessTokenResponse object.

Parameters
Returns
Example Usage
import {
  refreshAccessToken,
  ClientCredentials,
  AccessTokenResponse
} from 'steemconnect-firebase-functions';

const clientCredentials: ClientCredentials = {
  clientId: 'strimi.app',
  clientSecret: 'gfd65464fdsfsoi3o'
};
const accessToken: AccessTokenResponse = {
  access_token: '432432423r43roinmeofrekgkfgfd',
  expires_in: 640000,
  username: 'jakipatryk',
  refresh_token: 'sdfiodoifroei434.3fds'
};

refreshAccessToken({ ...clientCredentials, ...accessToken }).then(newTokens =>
  console.log('newTokens')
);
//  {
//      access_token: '...',
//      expires_in: 640000,
//      username 'jakipatryk'
//      refresh_token: '...'
//  }
Implementation

The implementation is available on Github.


getUserData

export declare function getUserData({
  access_token
}: AccessTokenResponse): Promise<UserData>;
Definition

Gets user data from a SteemConnect.

Parameters
Returns
  • (Promise<UserData>): the Promise object which resolves with user data
Example Usage
import {
  getUserData,
  AccessTokenResponse
} from 'steemconnect-firebase-functions';

const accessToken: AccessTokenResponse = {
  access_token: 'fdskfe.4324234',
  expires_in: 64000,
  username: 'jakipatryk'
};

getUserData(accessToken).then(userData => console.log(userData));
Implementation

The implementation is available on Github.


setUserData

export declare function setUserMetadata({
  access_token,
  metadata
}: AccessTokenResponse & {
  metadata: object;
}): Promise<UserData>;
Definition

Sets user metadata in a SteemConnect.

Parameters
  • config (AccessTokenResponse & metadata (object)): the configuration object with access token and new metadata
Returns
  • (Promise<UserData>): the Promise object which resolves with updated user data
Example Usage
import {
  setUserMetadata,
  AccessTokenResponse
} from 'steemconnect-firebase-functions';

const accessToken: AccessTokenResponse = {
  access_token: 'smt432fdsokdswpw.432jieod',
  expires_in: 64000,
  username: 'jakipatryk'
};
const metadata = {
  isBanned: true
};

setUserMetadata({ ...accessToken, metadata }).then(updatedUserData =>
  console.log(updatedUserData)
);
Implementation

The implementation is available on Github.


revokeAccessToken

export declare function revokeAccessToken({
  access_token
}: AccessTokenResponse): Promise<any>;
Definition

Revokes access token.

Parameters
  • accessToken (AccessTokenResponse): the AccessTokenResponse object containing access_token to revoke
Returns
  • (Promise): a Promise object
Example Usage
import {
  revokeAccessToken,
  AccessTokenResponse
} from 'steemconnect-firebase-functions';

const accessToken: AccessTokenResponse = {
  access_token: 'smt432fdsokdswpw.432jieod',
  expires_in: 64000,
  username: 'jakipatryk'
};

revokeAccessToken(accessToken).then(() =>
  console.log('Successfully revoked access token!')
);
Implementation

The implementation is available on Github.


Interfaces and types

ClientCredentials

Definition

Defines an object with client credentials.

Implementation
export interface ClientCredentials {
  clientId: string;
  clientSecret?: string;
}

Scope

Definition

A type containing all available scopes on SteemConnect.

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