{"size": 2652, "ext": "js", "max_stars_repo_path": "test-client/client.js", "max_stars_repo_name": "enepti/eth-auth", "max_stars_repo_stars_event_min_datetime": "2021-07-04T12:12:45.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-18T19:32:03.000Z", "max_issues_repo_path": "test-client/client.js", "max_issues_repo_name": "enepti/eth-auth", "max_issues_repo_issues_event_min_datetime": "2021-06-24T19:12:53.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-24T19:17:14.000Z", "max_forks_repo_path": "test-client/client.js", "max_forks_repo_name": "enepti/eth-auth", "max_forks_repo_forks_event_min_datetime": "2021-06-24T19:07:23.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-11T16:25:50.000Z"}
{"max_stars_count": 8, "max_issues_count": 2, "max_forks_count": 3, "avg_line_length": 27.9157894737, "max_line_length": 106, "alphanum_fraction": 0.6542232278}
ethereum.autoRefreshOnNetworkChange = false

// setup Web3 provider
const network = 'rinkeby'
const provider = new ethers.getDefaultProvider(network, {etherscan: '5TC41U8EV1GMIQKE3VV7CX1FJT1NE1BF6V'})

const init = () => {
    document.getElementById('auth').addEventListener('click', async () => {
      await auth()
    }, true)
    document.getElementById('refresh').addEventListener('click', async () => {
      await refresh()
    }, true)
}

const auth = async () => {
  // get challenge from API while using the current account from Metamask
  let req = new Request(`http://localhost:3000/login/${web3.eth.defaultAccount}`, {
    method: 'GET'
  })
  let res = await fetch(req)
  let data = await res.json()
  let sig = ""

  // make sure we're signing from the right account
  if (web3.eth.defaultAccount === data.address) {
    sig = await sign(data.challenge)
  }

  // reply with signed response
  req = new Request(`http://localhost:3000/login/${web3.eth.defaultAccount}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: `{"signature": "${sig}"}`
  })
  res = await fetch(req)
  data = await res.json();
  if (res.status === 200) {
    /// all good, think about storing the authz token
    allow(data.token)
  } else if (res.status === 403) {
    // forbidden
    deny(data)
  } else {
    throw new Error('Something went wrong on api server!');
  }
}

// refresh token
const refresh = async () => {
  req = new Request(`http://localhost:3000/refresh`, {
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${document.getElementById('token').innerText}`
    }
  })
  res = await fetch(req)
  data = await res.json();
  if (res.status === 200) {
    allow(data.token)
  } else if (res.status === 403) {
    deny(data)
  } else {
    throw new Error('Something went wrong on api server!');
  }
}

// sign message with Metamask
const sign = (msg) => {
  const signer = (new ethers.providers.Web3Provider(window.ethereum)).getSigner()
  return signer.signMessage(msg)
}

// update DOM when allowing access
const allow = (token) => {
  document.getElementById('not_allowed').hidden = true
  document.getElementById('allowed').hidden = false
  document.getElementById('token').innerText = token
}

// update DOM when denying access
const deny = (name) => {
  // name = (!name) ? '<em>Unregistered ENS name</em>' : name
  document.getElementById('allowed').hidden = true
  const p = document.createElement('p')
  p.style.color = 'red'
  p.innerHTML = `Not allowed`
  document.getElementById('not_allowed').appendChild(p)
  document.getElementById('not_allowed').hidden = false
}

init()
