{"size": 1698, "ext": "js", "max_stars_repo_path": "app/initializers/authentication.js", "max_stars_repo_name": "substantial/substantial-dash-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/initializers/authentication.js", "max_issues_repo_name": "substantial/substantial-dash-client", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/initializers/authentication.js", "max_forks_repo_name": "substantial/substantial-dash-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.96, "max_line_length": 94, "alphanum_fraction": 0.6283863369}
import Cookies from 'appkit/utils/cookies';

var AuthenticationInitializer = {
  name: "authentication",

  initialize: function(container, application) {
    var location = window.location;
    var url = window.purl(location.href);
    var isLogout = url.param('logout');
    
    if (isLogout === 'true') {
      Cookies.removeItem('api_key');
      Cookies.removeItem('user_name');
      Ember.Logger.debug('App.initializer authentication logged out');

    } else {
      var apiKey = url.param('api_key');
      var userName = url.param('user_name');

      if (Ember.isEmpty(apiKey)) {
        apiKey = Cookies.getItem('api_key');
        userName = Cookies.getItem('user_name');
      }

      if (!Ember.isEmpty(apiKey)) {
        var auth = Ember.Object.createWithMixins({
          apiKey: apiKey,
          userName: userName,
          logoutUrl: function() {
            return window.ENV.api_base_url + '/auth/logout?api_key=' +
              encodeURIComponent(this.get('apiKey'));
          }.property('apiKey')
        });
        Cookies.setItem('api_key', apiKey);
        Cookies.setItem('user_name', userName);
        application.register('auth:main', auth, { instantiate: false });
        application.inject('controller', 'auth', 'auth:main');
        application.inject('component', 'auth', 'auth:main');
        Ember.Logger.debug('App.initializer authentication succeeded: '+auth.get('userName'));
      }
    }

    // Clean the auth info querystring from the document's URL.
    var cleanPath = url.attr('path') + '#' + url.attr('fragment');
    window.history.replaceState("authenticationInitialized", "", cleanPath);
  }
};

export default AuthenticationInitializer;
