<commit_before>/* global API */
import { unescape } from 'plug-message-split'
import fetch from './fetch'

const jsonHeaders = {
  Accept: 'application/json',
  'Content-Type': 'application/json'
}

function getBlurb () {
  return fetch('/_/users/me', { credentials: 'same-origin' })
    .then(res => unescape(res.data[0].blurb))
}

function setBlurb (blurb) {
  return fetch('/_/profile/blurb', {
    credentials: 'same-origin',
    method: 'put',
    headers: jsonHeaders,
    body: JSON.stringify({ blurb: blurb })
  })
}

export default async function authenticate ({
  user = API.getUser().id,
  transport
} = {}) {
  let oldBlurb
  let result

  try {
    const data = await transport.getToken({ user })
    oldBlurb = await getBlurb()
    await setBlurb(`_auth_blurb=${data.blurb}`)
    result = await transport.verify({ user })
  } catch (e) {
    throw e
  } finally {
    if (oldBlurb) {
      await setBlurb(oldBlurb)
    }
  }

  if (result && result.token) {
    return result
  }
}<commit_msg>Work around rollup bug(?) which dropped this default-exported async function<commit_after>/* global API */
import { unescape } from 'plug-message-split'
import fetch from './fetch'

const jsonHeaders = {
  Accept: 'application/json',
  'Content-Type': 'application/json'
}

function getBlurb () {
  return fetch('/_/users/me', { credentials: 'same-origin' })
    .then(res => unescape(res.data[0].blurb))
}

function setBlurb (blurb) {
  return fetch('/_/profile/blurb', {
    credentials: 'same-origin',
    method: 'put',
    headers: jsonHeaders,
    body: JSON.stringify({ blurb: blurb })
  })
}

async function authenticate ({
  user = API.getUser().id,
  transport
} = {}) {
  let oldBlurb
  let result

  try {
    const data = await transport.getToken({ user })
    oldBlurb = await getBlurb()
    await setBlurb(`_auth_blurb=${data.blurb}`)
    result = await transport.verify({ user })
  } catch (e) {
    throw e
  } finally {
    if (oldBlurb) {
      await setBlurb(oldBlurb)
    }
  }

  if (result && result.token) {
    return result
  }
}

export default authenticate
{'fraction_non_alphanumeric': 0.1482544237207078, 'fraction_numerical': 0.0009564801530368245, 'mean_word_length': 3.2262626262626264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5517463', 'n_tokens_mistral': 701, 'n_tokens_neox': 700, 'n_words': 172}