import Ember from 'ember';
import { inject as service } from '@ember/service';

import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
import { task } from 'ember-concurrency';

import ENV from '../config/environment';


export default BaseAuthenticator.extend({
    session: service(),

    csrfToken() {
        if (!document.cookie && document.cookie === '') {
            return null;
        }
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, ENV.csrfCookie.length + 1) === `${ENV.csrfCookie}=`) {
                return decodeURIComponent(cookie.substring(ENV.csrfCookie.length + 1));
            }
        }
    },

    restore() {
        return this.authenticate(false);
    },

    authenticate(redirectToLogin = true) {
        return new Ember.RSVP.Promise((resolve, reject) => {
            const responseAttrs = this.get('getUserInfo').perform();

            if (!responseAttrs || !responseAttrs.token) {
                if (redirectToLogin) {
                    window.location = `${ENV.apiBaseUrl}/accounts/osf/login/?${Ember.$.param({ next: window.location.pathname + window.location.search })}`;
                    return;
                }
                reject('not logged in');
            } else {
                resolve({
                    user: responseAttrs,
                    csrfToken: this.csrfToken(),
                });
            }
        });
    },

    invalidate() {
        return $.ajax({
            method: 'POST',
            url: `${ENV.apiBaseUrl}/accounts/logout/`,
            crossDomain: true,
            xhrFields: { withCredentials: true },
        });
    },

    getUserInfo: task(function* () {
        const response = yield $.ajax({
            url: `${ENV.apiUrl}/userinfo`,
            crossDomain: true,
            xhrFields: { withCredentials: true },
        });
        return response.data.attributes;
    }),
});

@@ -23,60 +23,8 @@
 er';
-%0Aimport %7B inject as service %7D from '@ember/service';
 %0A%0Aim
@@ -224,33 +224,8 @@
 d(%7B%0A
-    session: service(),%0A%0A
     
@@ -875,30 +875,8 @@
     
- const responseAttrs =
  thi
@@ -908,523 +908,42 @@
 orm(
-);%0A%0A            if (!responseAttrs %7C%7C !responseAttrs.token) %7B%0A                if (redirectToLogin) %7B%0A                    window.location = %60$%7BENV.apiBaseUrl%7D/accounts/osf/login/?$%7BEmber.$.param(%7B next: window.location.pathname + window.location.search %7D)%7D%60;%0A                    return;%0A                %7D%0A                reject('not logged in');%0A            %7D else %7B%0A                resolve(%7B%0A                    user: responseAttrs,%0A                    csrfToken: this.csrfToken(),%0A                %7D);%0A            %7D
+resolve, reject, redirectToLogin);
 %0A   
@@ -1219,16 +1219,48 @@
 ction* (
+resolve, reject, redirectToLogin
 ) %7B%0A    
@@ -1443,40 +1443,520 @@
     
-return response.data.attributes;
+const responseAttrs = response.data.attributes;%0A%0A        if (!responseAttrs %7C%7C !responseAttrs.token) %7B%0A            if (redirectToLogin) %7B%0A                window.location = %60$%7BENV.apiBaseUrl%7D/accounts/osf/login/?$%7BEmber.$.param(%7B next: window.location.pathname + window.location.search %7D)%7D%60;%0A                return;%0A            %7D%0A            reject('not logged in');%0A        %7D else %7B%0A            resolve(%7B%0A                user: responseAttrs,%0A                csrfToken: this.csrfToken(),%0A            %7D);%0A        %7D
 %0A   

Fix the login (#206)