github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/controllers/settings/tokens.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { reads } from '@ember/object/computed';
     3  import Controller from '@ember/controller';
     4  import { getOwner } from '@ember/application';
     5  import { alias } from '@ember/object/computed';
     6  import { action } from '@ember/object';
     7  import classic from 'ember-classic-decorator';
     8  
     9  @classic
    10  export default class Tokens extends Controller {
    11    @service token;
    12    @service system;
    13    @service store;
    14  
    15    @reads('token.secret') secret;
    16  
    17    tokenIsValid = false;
    18    tokenIsInvalid = false;
    19    @alias('token.selfToken') tokenRecord;
    20  
    21    resetStore() {
    22      this.store.unloadAll();
    23    }
    24  
    25    @action
    26    clearTokenProperties() {
    27      this.token.setProperties({
    28        secret: undefined,
    29      });
    30      this.setProperties({
    31        tokenIsValid: false,
    32        tokenIsInvalid: false,
    33      });
    34      // Clear out all data to ensure only data the anonymous token is privileged to see is shown
    35      this.system.reset();
    36      this.resetStore();
    37      this.token.reset();
    38    }
    39  
    40    @action
    41    verifyToken() {
    42      const { secret } = this;
    43      const TokenAdapter = getOwner(this).lookup('adapter:token');
    44  
    45      this.set('token.secret', secret);
    46  
    47      TokenAdapter.findSelf().then(
    48        () => {
    49          // Clear out all data to ensure only data the new token is privileged to see is shown
    50          this.system.reset();
    51          this.resetStore();
    52  
    53          // Refetch the token and associated policies
    54          this.get('token.fetchSelfTokenAndPolicies')
    55            .perform()
    56            .catch();
    57  
    58          this.setProperties({
    59            tokenIsValid: true,
    60            tokenIsInvalid: false,
    61          });
    62        },
    63        () => {
    64          this.set('token.secret', undefined);
    65          this.setProperties({
    66            tokenIsValid: false,
    67            tokenIsInvalid: true,
    68          });
    69        }
    70      );
    71    }
    72  }