Skip to content

Commit

Permalink
feat: account.createToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
deyhle committed Jul 12, 2022
1 parent 2091f3c commit b7a71f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions runme.js
@@ -1,8 +1,17 @@
const { PublicAPI } = require('./lib/index');
const { PublicAPI, Accounts } = require('./lib/index');

(async () => {
const dm = new PublicAPI('beefbeef', 'stage').setToken('eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9...');

//const dm = new PublicAPI('beefbeef', 'stage').setToken('eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9...');
const accounts = new Accounts('stage').setToken('eyJhbGciOiJSUzUxMi....');

const apiToken = await accounts.createApiToken();
console.log(apiToken);
const account = await accounts.account(apiToken.accountID);
console.log(account);
const newToken = await account.createToken();
console.log(newToken);
const allTokens = await account.tokenList();
console.log(allTokens);
// TODO your magic

return 'done';
Expand Down
26 changes: 26 additions & 0 deletions src/resources/accounts/AccountResource.ts
Expand Up @@ -4,8 +4,11 @@ import Resource from '../Resource';
import TokenList from './TokenList';
import { environment } from '../../Core';
import TokenResource from './TokenResource';
import { tokenResponse } from '../../Accounts';
import { post } from '../../helper';

const relationsSymbol: any = Symbol.for('relations');
const environmentSymbol: any = Symbol.for('environment');

interface AccountResource {
accountID: string;
Expand Down Expand Up @@ -223,6 +226,29 @@ class AccountResource extends Resource {
return <Promise<TokenList>>this.resourceList('token');
}

/**
* Create an additional access token {@link tokenResponse} for this account.
* Only supported for API Keys.
*
* @example
* return account.createToken()
* .then(({ jwt, accountID, iat, exp}) => {
* // do something with `jwt` because it is not accessable later
* });
*
* @returns {Promise<{jwt: string, accountID: string, iat: number, exp: number}>} the created api
* token response.
* @throws {Error} if the account is not an API Key
*/
createToken(): Promise<tokenResponse> {
if (this.email || this.hasPassword || this.state !== 'active') {
throw new Error('Cannot create token for this account. Only API Token Accounts can get an addtional token.');
}
return this.follow('ec:account/tokens')
.then((request) => post(this[environmentSymbol], request))
.then(([tokenResponse]) => tokenResponse);
}

// TODO remove permission
}

Expand Down

0 comments on commit b7a71f9

Please sign in to comment.