<commit_before>import Ember from 'ember';

export default Ember.Service.extend({
  init() {
    var key = this.get('sessionStoreKey'),
        store = this.container.lookup('store:main'),
        model = store.modelFor('user'),
        adapter = store.adapterFor('application'),
        serializer = store.serializerFor(model);

    this.sessionStore.get(key).then((sessionData) => {
      if (sessionData && sessionData.userId &&
          sessionData.sessionToken && sessionData._response) {

        this.setProperties({
          userId: sessionData.userId,
          sessionToken: sessionData.sessionToken
        });

        // Create a user instance and push to store
        serializer.normalize(model, sessionData._response);
        store.push(model, sessionData._response);

        // Set adapter properties
        delete sessionData._response;
        adapter.setProperties(sessionData);
      }
    });
  },

  userId: null,
  sessionToken: null,
  sessionStoreKey: 'ember-parse:session',
  isAuthenticated: Ember.computed('userId', 'sessionToken', function() {
    if (Ember.isEmpty(this.get('userId')) || Ember.isEmpty(this.get('sessionToken'))) {
      return false;
    } else {
      return true;
    }
  }),

  authenticate(username, password) {
    var key = this.get('sessionStoreKey'),
        store = this.container.lookup('store:main'),
        model = store.modelFor('user'),
        adapter = store.adapterFor('application'),
        serializer = store.serializerFor(model);

      var data = {
        username: username,
        password: password,
        _method: 'GET'
      };

      return adapter.ajax(adapter.buildURL('login'), 'POST', {data: data})
        .then((response) => {
          var sessionData = {
            userId: response.objectId,
            sessionToken: response.sessionToken,
            _response: response
          };

          this.setProperties(sessionData);
          this.sessionStore.save(key, sessionData);

          // Set adapter properties
          delete sessionData._response;
          adapter.setProperties(sessionData);

          serializer.normalize(model, response);
          var record = store.push(model, response);

          return record;

        }, (reason) => {
          var err = `Code ${reason.responseJSON.code}: ${reason.responseJSON.error}`;
          return Ember.RSVP.reject(new Error(err));
        });
  },

  invalidate() {
    if (this.get('isAuthenticated')) {
      var key = this.get('sessionStoreKey'),
          store = this.container.lookup('store:main'),
          adapter = store.adapterFor('application');

      // Manually remove user from store
      store.find('user', this.get('userId')).then((user) => {
        user.unloadRecord();
      });

      var sessionData = {
        userId: null,
        sessionToken: null
      };

      this.setProperties(sessionData);
      adapter.setProperties(sessionData);

      return this.sessionStore.destroy(key);
    } else {
      return Ember.RSVP.resolve();
    }
  },

  sessionStore: {
    save(key, data) {
      return new Ember.RSVP.Promise(function(resolve) {
        resolve(localStorage.setItem(key, JSON.stringify(data)));
      });
    },

    get(key) {<commit_msg>Fix comment<commit_after>import Ember from 'ember';

export default Ember.Service.extend({
  init() {
    var key = this.get('sessionStoreKey'),
        store = this.container.lookup('store:main'),
        model = store.modelFor('user'),
        adapter = store.adapterFor('application'),
        serializer = store.serializerFor(model);

    this.sessionStore.get(key).then((sessionData) => {
      if (sessionData && sessionData.userId &&
          sessionData.sessionToken && sessionData._response) {

        this.setProperties({
          userId: sessionData.userId,
          sessionToken: sessionData.sessionToken
        });

        // Create a user instance and push to store
        serializer.normalize(model, sessionData._response);
        store.push(model, sessionData._response);

        // Set adapter properties
        delete sessionData._response;
        adapter.setProperties(sessionData);
      }
    });
  },

  userId: null,
  sessionToken: null,
  sessionStoreKey: 'ember-parse:session',
  isAuthenticated: Ember.computed('userId', 'sessionToken', function() {
    if (Ember.isEmpty(this.get('userId')) || Ember.isEmpty(this.get('sessionToken'))) {
      return false;
    } else {
      return true;
    }
  }),

  authenticate(username, password) {
    var key = this.get('sessionStoreKey'),
        store = this.container.lookup('store:main'),
        model = store.modelFor('user'),
        adapter = store.adapterFor('application'),
        serializer = store.serializerFor(model);

      var data = {
        username: username,
        password: password,
        _method: 'GET'
      };

      return adapter.ajax(adapter.buildURL('login'), 'POST', {data: data})
        .then((response) => {
          var sessionData = {
            userId: response.objectId,
            sessionToken: response.sessionToken,
            _response: response
          };

          this.setProperties(sessionData);
          this.sessionStore.save(key, sessionData);

          // Set adapter properties
          delete sessionData._response;
          adapter.setProperties(sessionData);

          serializer.normalize(model, response);
          var record = store.push(model, response);

          return record;

        }, (reason) => {
          var err = `Code ${reason.responseJSON.code}: ${reason.responseJSON.error}`;
          return Ember.RSVP.reject(new Error(err));
        });
  },

  invalidate() {
    if (this.get('isAuthenticated')) {
      var key = this.get('sessionStoreKey'),
          store = this.container.lookup('store:main'),
          adapter = store.adapterFor('application');

      // Remove user from store
      store.find('user', this.get('userId')).then((user) => {
        user.unloadRecord();
      });

      var sessionData = {
        userId: null,
        sessionToken: null
      };

      this.setProperties(sessionData);
      adapter.setProperties(sessionData);

      return this.sessionStore.destroy(key);
    } else {
      return Ember.RSVP.resolve();
    }
  },

  sessionStore: {
    save(key, data) {
      return new Ember.RSVP.Promise(function(resolve) {
        resolve(localStorage.setItem(key, JSON.stringify(data)));
      });
    },

    get(key) {
{'fraction_non_alphanumeric': 0.13259156235512284, 'fraction_numerical': 0.0, 'mean_word_length': 2.440723019670388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099931', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1767, 'n_words': 334}