Skip to content

Commit 349b2d0

Browse files
author
Kadi Kraman
committed
Update the library API to remove AppAuth class
1 parent 389d574 commit 349b2d0

File tree

3 files changed

+228
-177
lines changed

3 files changed

+228
-177
lines changed

README.md

Lines changed: 113 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
[![Build Status](https://travis-ci.org/FormidableLabs/react-native-app-auth.svg?branch=master)](https://travis-ci.org/FormidableLabs/react-native-app-auth)
88
[![npm version](https://badge.fury.io/js/react-native-app-auth.svg)](https://badge.fury.io/js/react-native-app-auth)
99

10+
## This is the API documentation for `react-native-app-auth >= 2.0.` See version `1.x` documentation [here](https://github.com/FormidableLabs/react-native-app-auth/tree/v1.0.1).
11+
1012
React Native bridge for [AppAuth-iOS](https://github.com/openid/AppAuth-iOS) and
1113
[AppAuth-Android](https://github.com/openid/AppAuth-Android) SDKS for communicating with
1214
[OAuth 2.0](https://tools.ietf.org/html/rfc6749) and
@@ -51,50 +53,81 @@ flow and returns the access token, refresh token and access token expiry date wh
5153
throws an error when not successful.
5254

5355
```js
54-
import AppAuth from 'react-native-app-auth';
56+
import { authorize } from 'react-native-app-auth';
57+
58+
const baseConfig = {
59+
issuer: '<YOUR_ISSUER_URL>',
60+
clientId: '<YOUR_CLIENT_ID>',
61+
redirectUrl: '<YOUR_REDIRECT_URL>',
62+
scopes: `<YOUR_SCOPES_ARRAY>`
63+
};
5564

56-
const appAuth = new AppAuth(config);
57-
const result = await appAuth.authorize(scopes);
58-
// returns accessToken, accessTokenExpirationDate and refreshToken
65+
const result = await authorize(baseConfig);
5966
```
6067

61-
#### `config`
68+
#### `baseConfig`
69+
70+
This is your configuration object for the client. The baseConfig is passed into each of the methods
71+
with optional overrides.
6272

63-
This is your configuration object for the client
64-
- **issuer** - (`string`) *REQUIRED* the url of the auth server
65-
- **clientId** - (`string`) *REQUIRED* your client id on the auth server
66-
- **redirectUrl** - (`string`) *REQUIRED* the url that links back to your app with the auth code
67-
- **additionalParameters** - (`object` | `null`) additional parameters that will be passed in the authorization request.
68-
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
69-
`hello=world&foo=bar` to the authorization request.
73+
* **issuer** - (`string`) _REQUIRED_ the url of the auth server
74+
* **clientId** - (`string`) _REQUIRED_ your client id on the auth server
75+
* **redirectUrl** - (`string`) _REQUIRED_ the url that links back to your app with the auth code
76+
* **scopes** - (`array<string>`) _REQUIRED_ the scopes for your token, e.g. `['email', 'offline_access']`
77+
* **additionalParameters** - (`object`) additional parameters that will be passed in the authorization request.
78+
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
79+
`hello=world&foo=bar` to the authorization request.
7080

71-
### `result`
81+
#### `result`
7282

7383
This is the result from the auth server
74-
- **accessToken** - (`string`) the access token
75-
- **accessTokenExpirationDate** - (`string`) the token expiration date
76-
- **additionalParameters** - (`Object`) additional url parameters from the auth server
77-
- **idToken** - (`string`) the id token
78-
- **refreshToken** - (`string`) the refresh token
79-
- **tokenType** - (`string`) the token type, e.g. Bearer
84+
85+
* **accessToken** - (`string`) the access token
86+
* **accessTokenExpirationDate** - (`string`) the token expiration date
87+
* **additionalParameters** - (`Object`) additional url parameters from the auth server
88+
* **idToken** - (`string`) the id token
89+
* **refreshToken** - (`string`) the refresh token
90+
* **tokenType** - (`string`) the token type, e.g. Bearer
8091

8192
### `refresh`
8293

8394
This method will refresh the accessToken using the refreshToken. Some auth providers will also give
8495
you a new refreshToken
8596

8697
```js
87-
const result = await appAuth.refresh(refreshToken, scopes);
88-
// returns accessToken, accessTokenExpirationDate and (maybe) refreshToken
98+
import { refresh } from 'react-native-app-auth';
99+
100+
const baseConfig = {
101+
issuer: '<YOUR_ISSUER_URL>',
102+
clientId: '<YOUR_CLIENT_ID>',
103+
redirectUrl: '<YOUR_REDIRECT_URL>',
104+
scopes: `<YOUR_SCOPES_ARRAY>`,
105+
};
106+
107+
const result = await refresh({
108+
...baseConfig,
109+
refreshToken: `<REFRESH_TOKEN>`,
110+
});
89111
```
90112

91-
### `revokeToken`
113+
### `revoke`
92114

93115
This method will revoke a token. The tokenToRevoke can be either an accessToken or a refreshToken
94116

95117
```js
96-
// note, sendClientId=true will only be required when using IdentityServer
97-
const result = await appAuth.revokeToken(tokenToRevoke, sendClientId);
118+
import { revoke } from 'react-native-app-auth';
119+
120+
const baseConfig = {
121+
issuer: '<YOUR_ISSUER_URL>',
122+
clientId: '<YOUR_CLIENT_ID>',
123+
redirectUrl: '<YOUR_REDIRECT_URL>',
124+
scopes: `<YOUR_SCOPES_ARRAY>`,
125+
};
126+
127+
const result = await revoke({
128+
...baseConfig,
129+
refreshToken: `<REFRESH_TOKEN>`
130+
});
98131
```
99132

100133
## Getting started
@@ -113,8 +146,7 @@ steps instead.
113146

114147
#### iOS
115148

116-
1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's
117-
name]`
149+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
118150
2. Go to `node_modules``react-native-app-auth` and add `RNAppAuth.xcodeproj`
119151
3. In XCode, in the project navigator, select your project. Add `libRNAppAuth.a` to your project's
120152
`Build Phases``Link Binary With Libraries`
@@ -299,19 +331,19 @@ The scheme is the beginning of your OAuth Redirect URL, up to the scheme separat
299331
## Usage
300332

301333
```javascript
302-
import AppAuth from 'react-native-app-auth';
334+
import { authorize } from 'react-native-app-auth';
303335

304-
// initialise the client with your configuration
305-
const appAuth = new AppAuth({
336+
// base config
337+
const baseConfig = {
306338
issuer: '<YOUR_ISSUER_URL>',
307339
clientId: '<YOUR_CLIENT_ID>',
308340
redirectUrl: '<YOUR_REDIRECT_URL>',
309-
});
341+
scopes: `<YOUR_SCOPES_ARRAY>`
342+
};
310343

311344
// use the client to make the auth request and receive the authState
312345
try {
313-
const scopes = ['profile'];
314-
const result = await appAuth.authorize(scopes);
346+
const result = await authorize(baseConfig);
315347
// result includes accessToken, accessTokenExpirationDate and refreshToken
316348
} catch (error) {
317349
console.log(error);
@@ -330,44 +362,53 @@ This library supports authenticating for Identity Server 4 out of the box. Some
330362

331363
```js
332364
// Note "offline_access" scope is required to get a refresh token
333-
const scopes = ["openid", "profile", "offline_access"];
334-
const appAuth = new AppAuth({
335-
issuer: "https://demo.identityserver.io",
336-
clientId: "native.code",
337-
redirectUrl: "io.identityserver.demo:/oauthredirect"
338-
});
365+
const baseConfig = {
366+
issuer: 'https://demo.identityserver.io',
367+
clientId: 'native.code',
368+
redirectUrl: 'io.identityserver.demo:/oauthredirect',
369+
scopes: ['openid', 'profile', 'offline_access']
370+
};
339371

340372
// Log in to get an authentication token
341-
const authState = await appAuth.authorize(scopes);
373+
const authState = await authorize(baseConfig);
342374

343375
// Refresh token
344-
const refreshedState = appAuth.refresh(authState.refreshToken, scopes);
376+
const refreshedState = refresh({ ...baseConfig, refreshToken: authState.refreshToken });
345377

346378
// Revoke token, note that Identity Server expects a client id on revoke
347-
const sendClientIdOnRevoke = true;
348-
await appAuth.revokeToken(refreshedState.refreshToken, sendClientIdOnRevoke);
379+
await appAuth.revoke({
380+
...baseConfig,
381+
tokenToRevoke: refreshedState.refreshToken,
382+
sendClientId: true
383+
});
349384
```
350385

351386
### Google
352387

353388
Full support out of the box.
354389

355390
```js
356-
const scopes = ["openid", "profile"];
357-
const appAuth = new AppAuth({
358-
issuer: "https://accounts.google.com",
359-
clientId: "GOOGLE_OAUTH_APP_GUID.apps.googleusercontent.com",
360-
redirectUrl: "com.googleusercontent.apps.GOOGLE_OAUTH_APP_GUID:/oauth2redirect/google"
361-
});
391+
const baseConfig = {
392+
issuer: 'https://accounts.google.com',
393+
clientId: 'GOOGLE_OAUTH_APP_GUID.apps.googleusercontent.com',
394+
redirectUrl: 'com.googleusercontent.apps.GOOGLE_OAUTH_APP_GUID:/oauth2redirect/google',
395+
scopes: ['openid', 'profile', 'offline_access']
396+
};
362397

363398
// Log in to get an authentication token
364-
const authState = await appAuth.authorize(scopes);
399+
const authState = await authorize(baseConfig);
365400

366401
// Refresh token
367-
const refreshedState = appAuth.refresh(authState.refreshToken, scopes);
402+
const refreshedState = appAuth.refresh({
403+
...baseConfig,
404+
refreshToken: authState.refreshToken
405+
});
368406

369407
// Revoke token
370-
await appAuth.revokeToken(refreshedState.refreshToken);
408+
await revoke({
409+
...baseConfig,
410+
tokenToRevoke: refreshedState.refreshToken
411+
});
371412
```
372413

373414
### Okta
@@ -381,21 +422,27 @@ Full support out of the box.
381422
> Click **Done** and you'll see a client ID on the next screen. Copy the redirect URI and clientId values into your App Auth config.
382423
383424
```js
384-
const scopes = ["openid", "profile"];
385-
const appAuth = new AppAuth({
425+
const baseConfig = {
386426
issuer: 'https://{yourOktaDomain}.com/oauth2/default',
387427
clientId: '{clientId}',
388-
redirectUrl: 'com.{yourReversedOktaDomain}:/callback'
389-
});
428+
redirectUrl: 'com.{yourReversedOktaDomain}:/callback',
429+
scopes: ['openid', 'profile']
430+
};
390431

391432
// Log in to get an authentication token
392-
const authState = await appAuth.authorize(scopes);
433+
const authState = await authorize(baseConfig);
393434

394435
// Refresh token
395-
const refreshedState = appAuth.refresh(authState.refreshToken, scopes);
436+
const refreshedState = await refresh({
437+
...baseConfig,
438+
refreshToken: authState.refreshToken,
439+
});
396440

397441
// Revoke token
398-
await appAuth.revokeToken(refreshedState.refreshToken);
442+
await revoke({
443+
...baseConfig,
444+
tokenToRevoke: refreshedState.refreshToken
445+
});
399446
```
400447

401448
### Keycloak
@@ -405,18 +452,21 @@ Keycloak [does not specify a revocation endpoint](http://keycloak-user.88327.x6.
405452
If you use [JHipster](http://www.jhipster.tech/)'s default Keycloak Docker image, everything will work with the following settings, except for revoke.
406453

407454
```js
408-
const scopes = ["openid", "profile"];
409-
const appAuth = new AppAuth({
455+
const baseConfig = {
410456
issuer: 'http://localhost:9080/auth/realms/jhipster',
411457
clientId: 'web_app',
412458
redirectUrl: '<YOUR_REDIRECT_SCHEME>:/callback'
413-
});
459+
scopes: ['openid', 'profile']
460+
};
414461

415462
// Log in to get an authentication token
416-
const authState = await appAuth.authorize(scopes);
463+
const authState = await authorize(baseConfig);
417464

418465
// Refresh token
419-
const refreshedState = appAuth.refresh(authState.refreshToken, scopes);
466+
const refreshedState = await refresh({
467+
...baseConfig,
468+
refreshToken: authState.refreshToken,
469+
});
420470
```
421471

422472
## Contributors

0 commit comments

Comments
 (0)