summaryrefslogtreecommitdiff
path: root/templates/portal.html
blob: a2959f1f5f750723940cc82f2ac5d2ce01e0ccaf (plain) (blame)
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
63
64
65
66
{{ define "content" }}
<p>welcome to ~penguins.lan</p>

{{ with .Portal }}
  {{ if .Taken }}
  <div class="info">
    <form action="/portal" method="post">
      <p><strong>{{ .RequestedName }}</strong> is already taken, last seen <strong>{{ .LastSeen }}</strong>. is that you?</p>
      <input type="hidden" name="username" value="{{ .RequestedName }}">
      <input type="hidden" name="reclaim" value="1">
      <button>yes, i am {{ .RequestedName }}</button>
    </form>
  </div>
  {{ end }}
  <br>
  <form action="/portal" method="post" id="loginForm">
    <label for="username">{{ if .Taken }} no? {{ end }} i want to be called</label>
    <input id="username" name="username" placeholder="{{ .SuggestedName }}" maxlength="24" autofocus>
    <button type="submit">enter</button>
  </form>

  {{ if .DetectedMAC }}
  <p class="muted"><small>this device: <code>{{ .DetectedMAC }}</code>.</small></p>
  {{ end }}
{{ end }}
{{ end }}

<script>
  const urlParamsText = window.location.href.split('?')[1];
  const urlParams = new URLSearchParams(urlParamsText);
  const paramsObj = Object.fromEntries(urlParams.entries());
  if (paramsObject['_token'] && paramsObject['_authaction']) {
    const form = document.getElementById("loginForm");
    const isEmpty = (formData, keys) => {
      const obj = Object.fromEntries(formData.entries());
      if (keys === undefined) keys = Object.keys(obj);
      return keys.every(key => !obj[key]);
    }
    const send = async () => {
      await fetch(paramsObject['_authaction'] + '?tok' + paramsObject['_token'], {
        mode: "no-cors"
      });
      const data = new FormData(form);
      await fetch(form.action, {
        method: form.method,
        body: data
      });
    };
    form.addEventListener("submit", (e) => {
      e.preventDefault();
      send().then(() => { window.location.href = "/"; });
    })

  }
  function getQueryVariable(variable) {
     query = window.location.search.substring(1);
     vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    } 
    alert('Query Variable ' + variable + ' not found');
  }
</script>