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