Skip to content

Commit

Permalink
Resolve async issue for authorization code flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marie Cart-Lamy committed Oct 17, 2019
1 parent dfd01f9 commit cca8591
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,41 @@ In the config include these parameters:
token: "https://auth.dataporten.no/oauth/token",
```

To resolve async issue after authorization, use `then()` method to return a Promise:

```
client.callback().then(callback => {
let token = null;
if (callback) {
token = callback;
console.log('I got the token', token);
} else {
client.getToken().then(tokenFromStore => {
token = tokenFromStore;
console.log('I got the token', token);
});
}
});
```

You can use async function and the `await` keyword:
```
async function MyFunction() {
let token = null;
const callback = await client.callback();
if (callback) {
token = callback;
} else {
token = await client.getToken();
}
console.log('I got the token', token);
}
```

Also be aware that the implementation of this flow uses `fetch`, to support older browser you would need to polyfill that.


Expand Down
5 changes: 4 additions & 1 deletion src/JSO.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,10 @@ class JSO extends EventEmitter {

} else if (response.hasOwnProperty("error")) {
throw this.processErrorResponse(response)
}

} else if (this.config.has('token')) {
return Promise.resolve()
}

}

Expand Down

0 comments on commit cca8591

Please sign in to comment.