React-native-app-auth: Proposal: v2.0.0

Created on 9 Feb 2018  路  3Comments  路  Source: FormidableLabs/react-native-app-auth

Get rid of the AppAuth class and make the api function-based

Currently API:

import AppAuth from 'react-native-app-auth';

const refreshTokenConfig = {
  issuer: <ISSUER>,
  redirectUrl: <REDIRECT_URL>,
  clientId: <CLIENT_ID>,
  additionalParameters: <ADDITIONAL_PARAMETERS>
};
const appAuth = new AppAuth(config);

// authorize
const scopes = [SCOPES];
const result = await appAuth.authorize(scopes);

// refresh
const scopes = [SCOPES];
const result = await appAuth.refresh(scopes);

// revoke
const tokenToRevoke = <TOKEN>;
const sendClientId = BOOLEAN;
const result = await appAuth.revokeToken(tokenToRevoke, sendClientId = false);

Proposed API:

import { authorize, refresh, revokeToken } from 'react-native-app-auth';

// authorize
const authorizeConfig = {
  issuer: <ISSUER>,
  redirectUrl: <REDIRECT_URL>,
  clientId: <CLIENT_ID>,
  scopes: [SCOPES],
  additionalParameters: <ADDITIONAL_PARAMETERS>
};
const result = await authorize(authorizeConfig);

// refresh
const refreshTokenConfig = {
  issuer: <ISSUER>,
  redirectUrl: <REDIRECT_URL>,
  clientId: <CLIENT_ID>,
  refreshToken: <TOKEN>,
  scopes: [SCOPES],
  additionalParameters: <ADDITIONAL_PARAMETERS>
};
const result = await refresh(refreshTokenConfig);

// revoke
const revokeTokenConfig = {
  issuer: <ISSUER>,
  clientId: <CLIENT_ID>,
  tokenToRevoke: <TOKEN>,
  sendClientId: BOOLEAN
};
const result = await revokeToken(revokeTokenConfig);

Pros

  • API is easier to grasp, as it gets rid of the extra step of creating a new instance of AppAuth
  • Easier to extend: the config is just an object

Cons

  • There is more repetition
  • Easier to lose track of shared config
  • Config validation has to be done in every exposed function rather than once when instantiating

Most helpful comment

How about allowing sharing the configuration for all three methods. This is basically exactly what you are doing, with the exception that the revoke method needs to be able to handle being passed fields it won't use (scopes, redirectUrl).

import { authorize, refresh, revoke } from 'react-native-app-auth';

// authorize
const baseConfig = {
  issuer: <ISSUER>,
  redirectUrl: <REDIRECT_URL>,
  clientId: <CLIENT_ID>,
  scopes: [SCOPES],
  additionalParameters: <ADDITIONAL_PARAMETERS>
};

// authenticate
const result = await authorize(baseConfig);

// refresh
const result = await refresh({
  ...baseConfig,
  refreshToken: result.refreshToken,
});

// revoke
const result = await revoke({
  ...baseConfig,
  tokenToRevoke: result.accessToken,
  sendClientId: BOOLEAN
});

All 3 comments

How about allowing sharing the configuration for all three methods. This is basically exactly what you are doing, with the exception that the revoke method needs to be able to handle being passed fields it won't use (scopes, redirectUrl).

import { authorize, refresh, revoke } from 'react-native-app-auth';

// authorize
const baseConfig = {
  issuer: <ISSUER>,
  redirectUrl: <REDIRECT_URL>,
  clientId: <CLIENT_ID>,
  scopes: [SCOPES],
  additionalParameters: <ADDITIONAL_PARAMETERS>
};

// authenticate
const result = await authorize(baseConfig);

// refresh
const result = await refresh({
  ...baseConfig,
  refreshToken: result.refreshToken,
});

// revoke
const result = await revoke({
  ...baseConfig,
  tokenToRevoke: result.accessToken,
  sendClientId: BOOLEAN
});

When you make this change, can you add a note to README saying something like "This is the API documentation for react-native-app-auth >= 2.0. See version 1.x documentation here." with a link to latest 1.x tag.

Done and v2.0.0 published 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pikooli picture pikooli  路  4Comments

Vamate picture Vamate  路  4Comments

baltuonis picture baltuonis  路  6Comments

dajaffe picture dajaffe  路  5Comments

DiegoRealpe picture DiegoRealpe  路  8Comments