Skip to content

Commit

Permalink
feat: allow callback for refresh (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosch authored Apr 24, 2023
1 parent 2da6984 commit 5e591e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ const strava = new Strava({
})()
```

### Refreshing the access token

This library will automatically refresh the access token when needed. You may need to store the refresh token somewhere, so you can use it `on_token_refresh` callback.
```javascript
import { Strava } from 'strava'

const strava = new Strava({
client_id: '123',
client_secret: 'abc',
refresh_token: 'def',
on_token_refresh: (response: RefreshTokenResponse) => {
db.set('refresh_token', response.refresh_token)
}
})
```

## Contributing

Issues and pull requests are welcome.
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Request {
}

this.response = (await response.json()) as RefreshTokenResponse
this.config.on_token_refresh?.(this.response)
}
return this.response
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface RefreshTokenRequest {
client_id: string
client_secret: string
refresh_token: string
on_token_refresh?: (token: RefreshTokenResponse) => void
}
export interface RefreshTokenResponse {
access_token: string
Expand Down

0 comments on commit 5e591e7

Please sign in to comment.