Skip to content

Commit b35e27c

Browse files
authored
Add support and documentation to use API keys (#724)
Simplify the API by adding a helper function to set the JWT only with kuzzle API key format.
1 parent 2033928 commit b35e27c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

doc/7/controllers/auth/create-api-key/snippets/create-api-key.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ try {
1818
*/
1919

2020
console.log('API key successfully created');
21+
22+
// Then use it with your client. Note: You don't need to call login after this because this bypasses the authentication system.
23+
kuzzle.setAPIKey(apiKey._source.token)
2124
} catch (e) {
2225
console.error(e);
2326
}

src/Kuzzle.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,19 @@ export class Kuzzle extends KuzzleEventEmitter {
620620
return this.protocol.connect();
621621
}
622622

623+
/**
624+
* Set this client to use a specific API key.
625+
*
626+
* After doing this you don't need to use login as it bypasses the authentication process.
627+
*/
628+
public setAPIKey(apiKey: string) {
629+
if (apiKey.match(/^kapikey-/) === null) {
630+
throw new Error("Invalid API key. Missing the `kapikey-` prefix.");
631+
}
632+
633+
this.jwt = apiKey;
634+
}
635+
623636
async _reconnect() {
624637
if (this._reconnectInProgress) {
625638
return;

0 commit comments

Comments
 (0)