Skip to content

Commit 2df621b

Browse files
committed
[DE-582] Implement renewAuthToken
Fixes #784.
1 parent 780af86 commit 2df621b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ This driver uses semantic versioning:
2222

2323
## Added
2424

25+
- Added `renewAuthToken` method to `Database` ([#784](https://github.com/arangodb/arangojs/issues/784))
26+
27+
This method allows refreshing the authentication token passed to the
28+
`useBearerAuth` method or used by the `login` method. Note that ArangoDB
29+
will currently only return a new token if the token is going to expire
30+
in the next 150 seconds.
31+
2532
- Added `returnOld` and `mergeObjects` to `CollectionInsertOptions` type
2633

2734
These options are only available when using `overwriteMode`.

src/database.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,34 @@ export class Database {
17931793
}
17941794
);
17951795
}
1796+
1797+
/**
1798+
* Attempts to renew the authentication token passed to {@link Database#useBearerAuth}
1799+
* or returned and used by {@link Database#login}. If a new authentication
1800+
* token is issued, it will be used for future requests and returned.
1801+
*
1802+
* @example
1803+
* ```js
1804+
* const db = new Database();
1805+
* await db.login("admin", "hunter2");
1806+
* // ... later ...
1807+
* const newToken = await db.renewAuthToken();
1808+
* if (!newToken) // no new token issued
1809+
* ```
1810+
*/
1811+
renewAuthToken(): Promise<string | null> {
1812+
return this.request(
1813+
{
1814+
method: "POST",
1815+
path: "/_open/auth/renew",
1816+
},
1817+
(res) => {
1818+
if (!res.body.jwt) return null;
1819+
this.useBearerAuth(res.body.jwt);
1820+
return res.body.jwt;
1821+
}
1822+
);
1823+
}
17961824
//#endregion
17971825

17981826
//#region databases

0 commit comments

Comments
 (0)