github.com/emate/nomad@v0.8.2-wo-binpacking/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  
     6  export default Controller.extend({
     7    token: service(),
     8    store: service(),
     9  
    10    secret: reads('token.secret'),
    11  
    12    tokenIsValid: false,
    13    tokenIsInvalid: false,
    14    tokenRecord: null,
    15  
    16    resetStore() {
    17      this.get('store').unloadAll();
    18    },
    19  
    20    actions: {
    21      clearTokenProperties() {
    22        this.get('token').setProperties({
    23          secret: undefined,
    24        });
    25        this.setProperties({
    26          tokenIsValid: false,
    27          tokenIsInvalid: false,
    28          tokenRecord: null,
    29        });
    30        this.resetStore();
    31      },
    32  
    33      verifyToken() {
    34        const { secret } = this.getProperties('secret', 'accessor');
    35        const TokenAdapter = getOwner(this).lookup('adapter:token');
    36  
    37        this.set('token.secret', secret);
    38  
    39        TokenAdapter.findSelf().then(
    40          token => {
    41            // Capture the token ID before clearing the store
    42            const tokenId = token.get('id');
    43  
    44            // Clear out all data to ensure only data the new token is privileged to
    45            // see is shown
    46            this.resetStore();
    47  
    48            // Immediately refetch the token now that the store is empty
    49            const newToken = this.get('store').findRecord('token', tokenId);
    50  
    51            this.setProperties({
    52              tokenIsValid: true,
    53              tokenIsInvalid: false,
    54              tokenRecord: newToken,
    55            });
    56          },
    57          () => {
    58            this.set('token.secret', undefined);
    59            this.setProperties({
    60              tokenIsValid: false,
    61              tokenIsInvalid: true,
    62              tokenRecord: null,
    63            });
    64          }
    65        );
    66      },
    67    },
    68  });