// login.js

// from 'utils.js'
/*   global openByWindowSetting */

// from 'popup.js'
/* loginSuccess, loginError */

$('#sign-up').click(signUp)
$('#forgot-password').click(forgotPassword)
$('#log-in').click(doLogin)
$('#logout-button').click(doLogout)

function signUp() {
  openByWindowSetting('https://archive.org/account/signup')
}

function forgotPassword() {
  openByWindowSetting('https://archive.org/account/forgot-password')
}

function doLogin(e) {
  e.preventDefault()
  $('#login-message').hide()
  let email = $('#email-address').val()
  let password = $('#password').val()
  if (email.length === 0) {
    $('#login-message').show().text('Please type an email')
    return
  }
  if (password.length === 0) {
    $('#login-message').show().text('Please type a password')
    return
  }
  $('#log-in').val('Please Wait...')
  const data = new URLSearchParams()
  data.append('username', email)
  data.append('password', password)
  data.append('action', 'login')
  const loginPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('timeout'))
    }, 5000)
    fetch('https://archive.org/account/login',
      {
        method: 'POST',
        body: data,
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
        .then(resolve, reject)
  })
  loginPromise
    .then(response => response.json())
    .then((res) => {
      if (res.status === 'bad_login') {
        $('#login-message').show().text('Incorrect Email or Password')
        $('#log-in').val('Login')
      } else {
        $('#login-message').show().css('color', 'green').text('Success')
        loginSuccess()
        setTimeout(() => {
          $('#login-page').hide()
          $('#setting-page').hide()
          $('#popup-page').show()
          $('#login-message').hide()
        }, 500)
        $('#log-in').val('Login')
      }
    })
    .catch(e => console.log(e))
}

function doLogout() {
  chrome.cookies.getAll({domain: '.archive.org'}, (cookies) => {
    for (var i = 0; i < cookies.length; i++) {
      if (cookies[i].name !== 'test-cookie') {
        chrome.cookies.remove({url: 'https://archive.org', name: cookies[i].name})
      }
    }
    $('#logout-button').hide()
    $('.tab-item').css('width', '22%')
    loginError()
  })
}
@@ -82,16 +82,25 @@
 p.js'%0A/*
+   global
  loginSu
@@ -839,16 +839,176 @@
 it...')%0A
+  // need to set test-cookie for login API to return json instead of html%0A  chrome.cookies.set(%7B url: 'https://archive.org', name: 'test-cookie', value: '1' %7D)%0A
   const 
@@ -1613,24 +1613,56 @@
 ((res) =%3E %7B%0A
+      $('#log-in').val('Login')%0A
       if (re
@@ -1764,42 +1764,8 @@
 d')%0A
-        $('#log-in').val('Login')%0A
     
@@ -2067,35 +2067,68 @@
     $('#
-log-in').val('Login
+email-address').val('')%0A        $('#password').val('
 ')%0A     
@@ -2148,20 +2148,30 @@
  .catch(
-e
+(e)
  =%3E
+ %7B%0A     
  console
@@ -2177,16 +2177,107 @@
 e.log(e)
+%0A      $('#login-message').show().text('Login Error')%0A      $('#log-in').val('Login')%0A    %7D
 )%0A%7D%0A%0Afun
@@ -2320,16 +2320,17 @@
 getAll(%7B
+ 
 domain: 
@@ -2343,16 +2343,17 @@
 ive.org'
+ 
 %7D, (cook
@@ -2487,16 +2487,17 @@
 remove(%7B
+ 
 url: 'ht
@@ -2537,16 +2537,17 @@
 %5Bi%5D.name
+ 
 %7D)%0A     
@@ -2644,12 +2644,13 @@
 ror()%0A  %7D)%0A%7D
+%0A

Clear login credentials & fixes login cookie bug (#657)