summaryrefslogtreecommitdiff
path: root/webapp/public/index.html
blob: fabc8a053d535fa98163fec570fda0e180c2c4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>

<body>
  <button onclick="connect()">Login with Web3</button>

  <script type="text/javascript">
    let account = null;
    let accessToken = null;
    const connect = async () => {
      if (window.ethereum) {
        await window.ethereum.send('eth_requestAccounts')
        window.w3 = new Web3(window.ethereum)
        var accounts = await w3.eth.getAccounts()
        account = accounts[0];

        accessToken = await authenticate()

        let opts = {
          method: 'GET',
          headers: {
            'Content-Type': "application/json",
            'Authorization': `Bearer ${accessToken}`
          }
        }

        let res = await fetch(`/secret`, opts)
        alert(await res.text())
      }
    }

    const authenticate = async () => {
      let res = await fetch(`/nonce?address=${account}`)
      let resBody = await res.json()

      let signature = await w3.eth.personal.sign(resBody.message, account)

      let opts = {
        method: 'POST',
        headers: {
          'Content-Type': "application/json",
          'Authorization': `Bearer ${resBody.tempToken}`
        }
      }

      res = await fetch(`/verify?signature=${signature}`, opts)
      resBody = await res.json()

      console.log(resBody)
      return resBody.token
    }
  </script>
</body>

</html>