Skip to content
Snippets Groups Projects
login.js 1.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • import Controller from '@ember/controller';
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    import { service } from '@ember/service';
    
    import { action } from '@ember/object';
    import { tracked } from '@glimmer/tracking';
    
    import { base_url } from 'papermerge/utils/host';
    
    Eugen Ciur's avatar
    Eugen Ciur committed
    
    
    export default class LoginController extends Controller {
      @tracked errorMessage;
    
      @tracked in_progress = false;
    
      @service session;
    
    Eugen Ciur's avatar
    Eugen Ciur committed
      @service router;
    
      async authenticate(username, password) {
    
          this.in_progress = true;
    
          await this.session.authenticate(
            'authenticator:auth-token',
    
            username,
    
            password
          );
        } catch (error) {
    
          if (error instanceof TypeError) {
            // TypeError is thrown by fetch API if there is a
            // network issue e.g. backend server is not reachable;
            let extra_message = `Please double check that REST API server '${base_url()}/' is reachable.`
            this.errorMessage = `${error} ${extra_message}`;
          } else {
            this.errorMessage = error;
          }
    
        } finally {
          this.in_progress = false;
    
    
        if (this.session.isAuthenticated) {
          // What to do with all this success?
    
    Eugen Ciur's avatar
    Eugen Ciur committed
          this.router.transitionTo('authenticated.index');
    
    
      get inProgress() {
        return this.in_progress;
      }
    
    
      get browsable_api_url() {
        return base_url();
      }