Skip to content

Commit 1f73a7b

Browse files
authored
Add a new device registration route (#121)
* added a new device registration form * removed unused variables * remove old register html * bind new device key from url * remove redirect * clear nodekey query string param after successuful addition * expand bool flipping * added documention on nodekey * remove null check * tweak wording
1 parent 8801214 commit 1f73a7b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

documentation/route_queries.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Route Queries
2+
3+
Some routes offer additional behavior to a route when a `?` exists in the URL. These are called query string parameters or route queries. Route queries are used to modify the behavior of a route. Below are the available route queries.
4+
5+
## Devices
6+
7+
/devices.html
8+
9+
### Parameters
10+
11+
#### `?nodekey={nodekey of a pending device}`
12+
13+
When this parameter exists, it will automatically open the New Device form and pre-fill the Device Key input automatically. Everything right of the `=` is used as the value of the input.
14+
15+
Below is an example of how to set up a redirect in NGINX from the default headscale /register/{nodekey} URL to utilize this parameter:
16+
17+
```nginx
18+
...
19+
20+
location /register/nodekey {
21+
rewrite ^/register/(.*)$ /web/devices.html?nodekey=$1 redirect;
22+
}
23+
24+
location /web {
25+
26+
...
27+
```

src/lib/devices/CreateDevice.svelte

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<script lang="ts">
22
import { fade } from 'svelte/transition';
3-
import { userStore, deviceStore } from '$lib/common/stores';
3+
import { userStore } from '$lib/common/stores';
44
import { getDevices, newDevice } from '$lib/common/apiFunctions.svelte';
55
import { alertStore } from '$lib/common/stores.js';
66
import { base } from '$app/paths';
7+
import { page } from '$app/stores';
8+
import { goto } from "$app/navigation";
79
810
// whether the new card html element is visible
911
export let newDeviceCardVisible = false;
12+
export let newDeviceKey = '';
1013
let newDeviceForm: HTMLFormElement;
11-
let newDeviceKey = '';
1214
let selectedUser = '';
1315
1416
let tabs = ['Default Configuration', 'With Preauth Keys', 'With OIDC'];
@@ -20,8 +22,15 @@
2022
.then((response) => {
2123
newDeviceCardVisible = false;
2224
newDeviceKey = '';
25+
2326
// refresh devices after editing
2427
getDevices();
28+
29+
// Clear device key in url
30+
if ($page.url.searchParams.get('nodekey')) {
31+
$page.url.searchParams.delete('nodekey');
32+
goto(`?${$page.url.searchParams.toString()}`);
33+
}
2534
})
2635
.catch((error) => {
2736
$alertStore = error;

src/routes/devices.html/+page.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- typescript -->
22
<script lang="ts">
33
import { base } from '$app/paths';
4+
import { page } from '$app/stores';
45
import { getDevices, getUsers } from '$lib/common/apiFunctions.svelte';
56
import { apiTestStore, deviceFilterStore, deviceStore } from '$lib/common/stores.js';
67
import CreateDevice from '$lib/devices/CreateDevice.svelte';
@@ -10,7 +11,9 @@
1011
import { onMount } from 'svelte';
1112
import { fade } from 'svelte/transition';
1213
13-
let newDeviceCardVisible = false;
14+
let newDeviceKey = $page.url.searchParams.get('nodekey') ?? '';
15+
16+
let newDeviceCardVisible = newDeviceKey.length > 0 ? true : false;
1417
1518
//
1619
// Component Variables
@@ -54,7 +57,7 @@
5457
>
5558
</table>
5659

57-
<CreateDevice bind:newDeviceCardVisible />
60+
<CreateDevice bind:newDeviceCardVisible bind:newDeviceKey />
5861

5962
{#each $deviceStore as device}
6063
{#if $deviceFilterStore.includes(device)}

0 commit comments

Comments
 (0)