Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: method to manually refresh token #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Un wiki est disponible, celui-ci rassemble davantage d'informations sur le fonct
* [.getAbsenceFile(folderId, params, includes)](#Skolengo+getAbsenceFile)
* [.postAbsenceFileState(folderId, reasonId, comment, params)](#Skolengo+postAbsenceFileState)
* [.getAbsenceReasons(limit, offset, params)](#Skolengo+getAbsenceReasons)
* [.refreshToken(triggerListener)](#Skolengo+refreshToken) ⇒
* _static_
* [.revokeToken(oidClient, token)](#Skolengo.revokeToken)
* [.getAppCurrentConfig()](#Skolengo.getAppCurrentConfig)
Expand Down Expand Up @@ -608,6 +609,18 @@ Skolengo.fromConfigObject(config).then(async user => {
})
})
```
<a name="Skolengo+refreshToken"></a>

### skolengo.refreshToken(triggerListener) ⇒
Demande un renouvellement du jeu de jetons

**Kind**: instance method of [<code>Skolengo</code>](#Skolengo)
**Returns**: Le nouveau token set

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| triggerListener | <code>boolean</code> | <code>true</code> | Si appeler la fonction définie par onTokenRefresh |

<a name="Skolengo.revokeToken"></a>

### Skolengo.revokeToken(oidClient, token)
Expand Down
20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,22 @@ export class Skolengo {
).data) as AbsenceReason[]
}

/**
* Demande un renouvellement du jeu de jetons
* @param {boolean} triggerListener Si appeler la fonction définie par onTokenRefresh
* @returns Le nouveau token set
*/
public async refreshToken (triggerListener = true): Promise<TokenSet> {
const newTokenSet = await this.oidClient.refresh(this.tokenSet)

if (triggerListener) {
this.config.onTokenRefresh(newTokenSet)
}

this.tokenSet = newTokenSet
return newTokenSet
}

/**
* Gérer l'erreur *PRONOTE_RESOURCES_NOT_READY* obtenue lorsque Skolengo tente d'obtenir les dernières notes d'un élève.
* Ce comportement peut être activé en modifiant le paramètre optionnel correspondant.
Expand Down Expand Up @@ -1051,9 +1067,7 @@ export class Skolengo {
const response = error.response
if (response === undefined) throw error
if (response.status === 401) {
const newTokenSet = await this.oidClient.refresh(this.tokenSet)
this.config.onTokenRefresh(newTokenSet)
this.tokenSet = newTokenSet
const newTokenSet = await this.refreshToken()
return await this.config.httpClient.request<T, R, D>({
...axiosConfig,
headers: {
Expand Down
Loading