Skip to content

Commit 4592fc0

Browse files
committed
Renamed function to getNewAccessTokenUsingRefreshToken and added more documentation in the README
1 parent 9334e7c commit 4592fc0

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ fastify.register(oauthPlugin, {
3131
})
3232

3333
fastify.get('/login/facebook/callback', async function (request, reply) {
34-
const result = await this.getAccessTokenFromAuthorizationCodeFlow(request)
34+
const token = await this.getAccessTokenFromAuthorizationCodeFlow(request)
3535

36-
console.log(result.access_token)
36+
console.log(token.access_token)
3737

3838
// if later you need to refresh the token you can use
39-
// this.getNewAccessTokenWithRefreshToken(result.refresh_token)
39+
// const newToken = await this.getNewAccessTokenUsingRefreshToken(token.refresh_token)
4040

41-
reply.send({ access_token: result.access_token })
41+
reply.send({ access_token: token.access_token })
4242
})
4343
```
4444

@@ -51,6 +51,17 @@ See [facebook example](./examples/facebook.js) for an example.
5151
This fastify plugin decorates the fastify instance with the [`simple-oauth2`](https://github.com/lelylan/simple-oauth2)
5252
instance.
5353

54+
## Utilities
55+
56+
This fastify plugin adds 2 utility decorators to your fastify instance:
57+
58+
- `getAccessTokenFromAuthorizationCodeFlow(request, callback)`: A function that uses the Authorization code flow to fetch an OAuth2 token using the data in the last request of the flow. If the callback is not passed it will return a promise. The object resulting from the callback call or the promise resolution is a *token response* object containing the following keys:
59+
- `access_token`
60+
- `refresh_token` (optional, only if the `offline scope` was originally requested)
61+
- `token_type` (generally `'bearer'`)
62+
- `expires_in` (number of seconds for the token to expire, e.g. `240000`)
63+
- `getNewAccessTokenUsingRefreshToken(refreshToken, params, callback)`: A function that takes a refresh token and retrieves a new *token response* object. This is generally useful with background processing workers to re-issue a new token when the original token has expired. The `params` argument is optional and it's an object that can be used to pass in extra parameters to the refresh request (e.g. a stricter set of scopes). If the callback is not passed this function will return a promise. The object resulting from the callback call or the promise resolution is a new *token response* object (see fields above).
64+
5465
## License
5566

5667
Licensed under [MIT](./LICENSE).

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,25 @@ const oauthPlugin = fp(function (fastify, options, next) {
9494
getAccessTokenFromAuthorizationCodeFlowCallbacked(request, callback)
9595
}
9696

97-
function getNewAccessTokenWithRefreshTokenCallbacked (refreshToken, params, callback) {
97+
function getNewAccessTokenUsingRefreshTokenCallbacked (refreshToken, params, callback) {
9898
const accessToken = fastify[name].accessToken.create({ refresh_token: refreshToken })
9999
accessToken.refresh(params, callback)
100100
}
101-
const getNewAccessTokenWithRefreshTokenPromisified = promisify(getNewAccessTokenWithRefreshTokenCallbacked)
101+
const getNewAccessTokenUsingRefreshTokenPromisified = promisify(getNewAccessTokenUsingRefreshTokenCallbacked)
102102

103-
function getNewAccessTokenWithRefreshToken (refreshToken, params, callback) {
103+
function getNewAccessTokenUsingRefreshToken (refreshToken, params, callback) {
104104
if (!callback) {
105-
return getNewAccessTokenWithRefreshTokenPromisified(refreshToken, params)
105+
return getNewAccessTokenUsingRefreshTokenPromisified(refreshToken, params)
106106
}
107-
getNewAccessTokenWithRefreshTokenCallbacked(refreshToken, params, callback)
107+
getNewAccessTokenUsingRefreshTokenCallbacked(refreshToken, params, callback)
108108
}
109109

110110
const oauth2 = oauth2Module.create(credentials)
111111

112112
if (startRedirectPath) {
113113
fastify.get(startRedirectPath, startRedirectHandler)
114114
fastify.decorate('getAccessTokenFromAuthorizationCodeFlow', getAccessTokenFromAuthorizationCodeFlow)
115-
fastify.decorate('getNewAccessTokenWithRefreshToken', getNewAccessTokenWithRefreshToken)
115+
fastify.decorate('getNewAccessTokenUsingRefreshToken', getNewAccessTokenUsingRefreshToken)
116116
}
117117

118118
try {

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ t.test('fastify-oauth2', t => {
8484
if (err) throw err
8585

8686
// attempts to refresh the token
87-
this.getNewAccessTokenWithRefreshToken(result.refresh_token, undefined, (err, result) => {
87+
this.getNewAccessTokenUsingRefreshToken(result.refresh_token, undefined, (err, result) => {
8888
if (err) throw err
8989

9090
const newToken = result
@@ -125,7 +125,7 @@ t.test('fastify-oauth2', t => {
125125
return this.getAccessTokenFromAuthorizationCodeFlow(request)
126126
.then(result => {
127127
// attempts to refresh the token
128-
return this.getNewAccessTokenWithRefreshToken(result.refresh_token)
128+
return this.getNewAccessTokenUsingRefreshToken(result.refresh_token)
129129
})
130130
.then(token => {
131131
return {

0 commit comments

Comments
 (0)