|
| 1 | +<script lang="ts"> |
| 2 | + import { enhance } from '$app/forms'; |
| 3 | + import { goto } from '$app/navigation'; |
| 4 | + import 'bulma/css/bulma.min.css'; |
| 5 | + import { PUBLIC_API_URL } from '$env/static/public'; |
| 6 | +
|
| 7 | + let loading = false; |
| 8 | + let error = ''; |
| 9 | + let showCreatedUser = false; |
| 10 | + let createdUser = null; |
| 11 | +
|
| 12 | + async function handleSubmit(event: SubmitEvent) { |
| 13 | + loading = true; |
| 14 | + error = ''; |
| 15 | +
|
| 16 | + try { |
| 17 | + const form = event.target as HTMLFormElement; |
| 18 | + const formData = new FormData(form); |
| 19 | +
|
| 20 | + const response = await fetch(`${PUBLIC_API_URL}/users`, { |
| 21 | + method: 'POST', |
| 22 | + body: JSON.stringify({ |
| 23 | + username: formData.get('username'), |
| 24 | + password: formData.get('password'), |
| 25 | + token: formData.get('token') |
| 26 | + }), |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json' |
| 29 | + } |
| 30 | + }); |
| 31 | +
|
| 32 | + if (!response.ok) { |
| 33 | + const data = await response.json(); |
| 34 | + throw new Error(JSON.stringify(data.detail) || 'Failed to create user'); |
| 35 | + } |
| 36 | +
|
| 37 | + createdUser = await response.json(); |
| 38 | + showCreatedUser = true; |
| 39 | + localStorage.setItem('created_user_email', createdUser.email); |
| 40 | + } catch (e) { |
| 41 | + error = e.message; |
| 42 | + } finally { |
| 43 | + loading = false; |
| 44 | + } |
| 45 | + } |
| 46 | +</script> |
| 47 | + |
| 48 | +<div class="section"> |
| 49 | + <div class="container"> |
| 50 | + <div class="columns is-centered"> |
| 51 | + <div class="column is-half"> |
| 52 | + <div class="box"> |
| 53 | + <h1 class="title has-text-centered">Create User</h1> |
| 54 | + |
| 55 | + {#if error} |
| 56 | + <div class="notification is-danger"> |
| 57 | + {error} |
| 58 | + </div> |
| 59 | + {/if} |
| 60 | + |
| 61 | + <form on:submit|preventDefault={handleSubmit}> |
| 62 | + <div class="field"> |
| 63 | + <label class="label" for="username">Username</label> |
| 64 | + <div class="control"> |
| 65 | + <input |
| 66 | + class="input" |
| 67 | + type="text" |
| 68 | + name="username" |
| 69 | + id="username" |
| 70 | + placeholder="user" |
| 71 | + required |
| 72 | + /> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div class="field"> |
| 77 | + <label class="label" for="name">Password</label> |
| 78 | + <div class="control"> |
| 79 | + <input |
| 80 | + class="input" |
| 81 | + type="password" |
| 82 | + name="password" |
| 83 | + id="password" |
| 84 | + placeholder="John Doe" |
| 85 | + required |
| 86 | + /> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div class="field"> |
| 91 | + <label class="label" for="token">Organization Token</label> |
| 92 | + <div class="control"> |
| 93 | + <input |
| 94 | + class="input" |
| 95 | + type="text" |
| 96 | + name="token" |
| 97 | + id="token" |
| 98 | + placeholder="looong string" |
| 99 | + value={localStorage.getItem('org_token') || ''} |
| 100 | + required |
| 101 | + /> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div class="field mt-5"> |
| 106 | + <div class="control"> |
| 107 | + <button |
| 108 | + class="button is-primary is-fullwidth {loading ? 'is-loading' : ''}" |
| 109 | + type="submit" |
| 110 | + disabled={loading} |
| 111 | + > |
| 112 | + Create User |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </form> |
| 117 | + </div> |
| 118 | + |
| 119 | + {#if showCreatedUser} |
| 120 | + <div class="card"> |
| 121 | + <div class="has-text-centered mt-4"> |
| 122 | + <p>User ID: {createdUser.id}</p> |
| 123 | + <p>Email: {createdUser.email}</p> |
| 124 | + <p>Password: {createdUser.password}</p> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + <div class="has-text-centered mt-4"> |
| 128 | + <a href="/login" class="has-text-grey"> Login </a> |
| 129 | + </div> |
| 130 | + {/if} |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | +</div> |
| 135 | + |
| 136 | +<style> |
| 137 | + .box { |
| 138 | + margin-top: 2rem; |
| 139 | + } |
| 140 | +
|
| 141 | + .title { |
| 142 | + margin-bottom: 2rem; |
| 143 | + } |
| 144 | +
|
| 145 | + .field { |
| 146 | + margin-bottom: 1.5rem; |
| 147 | + } |
| 148 | +
|
| 149 | + .button.is-primary { |
| 150 | + background-color: #00d1b2; |
| 151 | + transition: background-color 0.2s ease; |
| 152 | + } |
| 153 | +
|
| 154 | + .button.is-primary:hover { |
| 155 | + background-color: #00c4a7; |
| 156 | + } |
| 157 | +
|
| 158 | + .mt-4 { |
| 159 | + margin-top: 1.5rem; |
| 160 | + } |
| 161 | +
|
| 162 | + .mt-5 { |
| 163 | + margin-top: 2rem; |
| 164 | + } |
| 165 | +</style> |
0 commit comments