<commit_before><!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/app-storage/app-localstorage/app-localstorage-document.html">

<link rel="import" href="../lancie-login/lancie-ajax.html">

<dom-module id="lancie-auth-storage">
  <template>

    <app-localstorage-document id="storage" key="auth" data="{{auth}}" session-only></app-localstorage-document>

    <lancie-ajax
      id="currentUser"
      refurl="users/current"
      on-response="storeUser"></lancie-ajax>

  </template>
  <script>
  (function() {
    'use strict';

    Polymer({
      is: 'lancie-auth-storage',

      properties: {
        user: {
          type: Object,
          notify: true,
        },
        auth: {
          type: Object,
          observer: '_changed',
          value: function() {
            return {};
          }
        },
        initial: {
          type: Boolean,
          value: true
        },
      },

      getToken: function() {
        return this.auth && this.auth.token;
      },

      storeToken: function(token) {
        this.auth = {
          token: token
        };
      },

      storeUser: function(e, request) {
        // If the request fails, no further action is required. The token has expired and is no longer valid. The user is required to log in as usual.
        if (request.succeeded) {
          if (request.response.message === 'Not logged in') {
            return;
          }
          this.user = request.response;
        } else {
          // Old token can be cleared when the token is no longer valid
          this.auth = {};
        }
      },

      _changed: function(auth) {
        var token = this.getToken();
        if (!this.user && this.initial && token) {
          this.$.currentUser.injectToken(token);
          this.$.currentUser.generateRequest();
          this.initial = false;
        }
      },

    });
  })();
  </script>
</dom-module><commit_msg>Fix currentuser request being sent double<commit_after><!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/app-storage/app-localstorage/app-localstorage-document.html">

<link rel="import" href="../lancie-login/lancie-ajax.html">

<dom-module id="lancie-auth-storage">
  <template>

    <app-localstorage-document id="storage" key="auth" data="{{auth}}" session-only></app-localstorage-document>

    <lancie-ajax
      id="currentUser"
      refurl="users/current"
      on-response="storeUser"></lancie-ajax>

  </template>
  <script>
  (function() {
    'use strict';

    Polymer({
      is: 'lancie-auth-storage',

      properties: {
        user: {
          type: Object,
          notify: true,
        },
        auth: {
          type: Object,
          observer: '_changed',
          value: function() {
            return {};
          }
        },
        initial: {
          type: Boolean,
          value: true
        },
      },

      getToken: function() {
        return this.auth && this.auth.token;
      },

      storeToken: function(token) {
        this.initial = false;
        this.auth = {
          token: token
        };
      },

      storeUser: function(e, request) {
        // If the request fails, no further action is required. The token has expired and is no longer valid. The user is required to log in as usual.
        if (request.succeeded) {
          if (request.response.message === 'Not logged in') {
            return;
          }
          this.user = request.response;
        } else {
          // Old token can be cleared when the token is no longer valid
          this.auth = {};
        }
      },

      _changed: function(auth) {
        var token = this.getToken();
        if (!this.user && this.initial && token) {
          this.initial = false;
          this.$.currentUser.injectToken(token);
          this.$.currentUser.generateRequest();
        }
      },

    });
  })();
  </script>
</dom-module>
{'fraction_non_alphanumeric': 0.12985725614591595, 'fraction_numerical': 0.0015860428231562252, 'mean_word_length': 2.129652605459057, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '5820800', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1395, 'n_words': 396}