@mrgnw/anahtar 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/components/AuthFlow.svelte +351 -0
- package/dist/components/AuthFlow.svelte.d.ts +7 -0
- package/dist/components/OtpInput.svelte +102 -0
- package/dist/components/OtpInput.svelte.d.ts +11 -0
- package/dist/components/PasskeyPrompt.svelte +168 -0
- package/dist/components/PasskeyPrompt.svelte.d.ts +8 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +4 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +15 -0
- package/dist/db/d1.d.ts +22 -0
- package/dist/db/d1.js +202 -0
- package/dist/db/postgres.d.ts +12 -0
- package/dist/db/postgres.js +204 -0
- package/dist/db/sqlite.d.ts +7 -0
- package/dist/db/sqlite.js +187 -0
- package/dist/device.d.ts +1 -0
- package/dist/device.js +36 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +14 -0
- package/dist/kit/handle.d.ts +3 -0
- package/dist/kit/handle.js +18 -0
- package/dist/kit/handlers.d.ts +8 -0
- package/dist/kit/handlers.js +183 -0
- package/dist/otp.d.ts +6 -0
- package/dist/otp.js +35 -0
- package/dist/passkey.d.ts +20 -0
- package/dist/passkey.js +112 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +31 -0
- package/dist/types.d.ts +93 -0
- package/dist/types.js +1 -0
- package/package.json +92 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mrgnw
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @mrgnw/anahtar
|
|
2
|
+
|
|
3
|
+
> **Pre-alpha.** API may change between releases.
|
|
4
|
+
|
|
5
|
+
Auth for SvelteKit. Email+OTP + optional passkeys.
|
|
6
|
+
|
|
7
|
+
"Anahtar" = key (Turkish)
|
|
8
|
+
|
|
9
|
+
## Auth flow
|
|
10
|
+
|
|
11
|
+
1. Email -> OTP sent via your `onSendOTP` callback
|
|
12
|
+
2. OTP verified -> session cookie
|
|
13
|
+
3. First login -> passkey registration prompt
|
|
14
|
+
4. Future logins -> passkey autofill, OTP fallback
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pnpm add @mrgnw/anahtar
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Create your auth instance:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// src/lib/server/auth.ts
|
|
26
|
+
import { createAuth } from "@mrgnw/anahtar";
|
|
27
|
+
import { sqliteAdapter } from "@mrgnw/anahtar/sqlite";
|
|
28
|
+
import Database from "better-sqlite3";
|
|
29
|
+
|
|
30
|
+
const db = new Database("data/app.db");
|
|
31
|
+
|
|
32
|
+
export const auth = createAuth({
|
|
33
|
+
db: sqliteAdapter(db),
|
|
34
|
+
onSendOTP: async (email, code) => {
|
|
35
|
+
console.log(`[dev] OTP for ${email}: ${code}`);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Wire into SvelteKit:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// src/hooks.server.ts
|
|
44
|
+
import { auth } from "$lib/server/auth";
|
|
45
|
+
export const handle = auth.handle;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// src/routes/api/auth/[...path]/+server.ts
|
|
50
|
+
import { auth } from "$lib/server/auth";
|
|
51
|
+
export const { GET, POST } = auth.handlers;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Optional UI component:
|
|
55
|
+
|
|
56
|
+
```svelte
|
|
57
|
+
<script>
|
|
58
|
+
import { AuthFlow } from '@mrgnw/anahtar/components';
|
|
59
|
+
import { goto } from '$app/navigation';
|
|
60
|
+
</script>
|
|
61
|
+
|
|
62
|
+
<AuthFlow onSuccess={() => goto('/')} />
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Tests
|
|
66
|
+
|
|
67
|
+
68 tests: 46 unit (node) + 22 component (happy-dom).
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
pnpm test:unit
|
|
71
|
+
pnpm test:browser
|
|
72
|
+
pnpm test
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Docs
|
|
76
|
+
|
|
77
|
+
- [Integration guide](docs/integration.md) — install, config, theming, Postgres setup
|
|
78
|
+
- [PLAN.md](PLAN.md) — architecture, DB adapter interface, test setup
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onDestroy, onMount } from 'svelte';
|
|
3
|
+
import { guessDeviceName } from '../device.js';
|
|
4
|
+
import OtpInput from './OtpInput.svelte';
|
|
5
|
+
import PasskeyPrompt from './PasskeyPrompt.svelte';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
apiBase?: string;
|
|
9
|
+
onSuccess?: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { apiBase = '/api/auth', onSuccess }: Props = $props();
|
|
13
|
+
|
|
14
|
+
let step = $state<1 | 2 | 3 | 4>(1);
|
|
15
|
+
let congratsTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
16
|
+
let email = $state('');
|
|
17
|
+
let loading = $state(false);
|
|
18
|
+
let error = $state('');
|
|
19
|
+
let otpInput = $state<OtpInput>();
|
|
20
|
+
|
|
21
|
+
let conditionalAbort: AbortController | null = null;
|
|
22
|
+
|
|
23
|
+
onMount(() => {
|
|
24
|
+
tryConditionalWebAuthn();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
onDestroy(() => {
|
|
28
|
+
conditionalAbort?.abort();
|
|
29
|
+
if (congratsTimeout) clearTimeout(congratsTimeout);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
async function tryConditionalWebAuthn() {
|
|
33
|
+
try {
|
|
34
|
+
const { startAuthentication } = await import('@simplewebauthn/browser');
|
|
35
|
+
const res = await fetch(`${apiBase}/passkey/login-start`);
|
|
36
|
+
if (!res.ok) return;
|
|
37
|
+
const options = await res.json();
|
|
38
|
+
conditionalAbort = new AbortController();
|
|
39
|
+
const authResponse = await startAuthentication({
|
|
40
|
+
optionsJSON: options,
|
|
41
|
+
useBrowserAutofill: true
|
|
42
|
+
});
|
|
43
|
+
const verifyRes = await fetch(`${apiBase}/passkey/login-finish`, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
headers: { 'Content-Type': 'application/json' },
|
|
46
|
+
body: JSON.stringify(authResponse)
|
|
47
|
+
});
|
|
48
|
+
if (verifyRes.ok) {
|
|
49
|
+
onSuccess?.();
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
// Passkey autofill not available or cancelled
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function handleEmailSubmit() {
|
|
57
|
+
error = '';
|
|
58
|
+
if (!email.includes('@')) {
|
|
59
|
+
error = 'Please enter a valid email address.';
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
loading = true;
|
|
63
|
+
conditionalAbort?.abort();
|
|
64
|
+
conditionalAbort = null;
|
|
65
|
+
try {
|
|
66
|
+
const res = await fetch(`${apiBase}/start`, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: { 'Content-Type': 'application/json' },
|
|
69
|
+
body: JSON.stringify({ email })
|
|
70
|
+
});
|
|
71
|
+
if (!res.ok) {
|
|
72
|
+
const data = await res.json().catch(() => null);
|
|
73
|
+
error = data?.error ?? `Request failed (${res.status})`;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
step = 2;
|
|
77
|
+
} catch {
|
|
78
|
+
error = 'Something went wrong. Please try again.';
|
|
79
|
+
} finally {
|
|
80
|
+
loading = false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function handleOtpComplete(code: string) {
|
|
85
|
+
error = '';
|
|
86
|
+
loading = true;
|
|
87
|
+
try {
|
|
88
|
+
const res = await fetch(`${apiBase}/verify`, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: { 'Content-Type': 'application/json' },
|
|
91
|
+
body: JSON.stringify({ email, code })
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
const data = await res.json().catch(() => null);
|
|
95
|
+
error = data?.error ?? 'Invalid code. Please try again.';
|
|
96
|
+
otpInput?.clear();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const data = await res.json();
|
|
100
|
+
if (data.hasPasskey || data.skipPasskeyPrompt) {
|
|
101
|
+
onSuccess?.();
|
|
102
|
+
} else {
|
|
103
|
+
step = 3;
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
error = 'Something went wrong. Please try again.';
|
|
107
|
+
} finally {
|
|
108
|
+
loading = false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function resendCode() {
|
|
113
|
+
error = '';
|
|
114
|
+
loading = true;
|
|
115
|
+
try {
|
|
116
|
+
const res = await fetch(`${apiBase}/start`, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: { 'Content-Type': 'application/json' },
|
|
119
|
+
body: JSON.stringify({ email })
|
|
120
|
+
});
|
|
121
|
+
if (!res.ok) {
|
|
122
|
+
const data = await res.json().catch(() => null);
|
|
123
|
+
error = data?.error ?? 'Failed to resend code.';
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
otpInput?.clear();
|
|
127
|
+
} catch {
|
|
128
|
+
error = 'Something went wrong. Please try again.';
|
|
129
|
+
} finally {
|
|
130
|
+
loading = false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function handlePasskeyRegister() {
|
|
135
|
+
const { startRegistration } = await import('@simplewebauthn/browser');
|
|
136
|
+
const optRes = await fetch(`${apiBase}/passkey/register-start`, { method: 'POST' });
|
|
137
|
+
if (!optRes.ok) throw new Error('Failed to get registration options');
|
|
138
|
+
const options = await optRes.json();
|
|
139
|
+
|
|
140
|
+
const regResponse = await startRegistration({ optionsJSON: options });
|
|
141
|
+
const res = await fetch(`${apiBase}/passkey/register-finish`, {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: { 'Content-Type': 'application/json' },
|
|
144
|
+
body: JSON.stringify({ ...regResponse, name: guessDeviceName() })
|
|
145
|
+
});
|
|
146
|
+
if (!res.ok) throw new Error('Registration failed');
|
|
147
|
+
step = 4;
|
|
148
|
+
congratsTimeout = setTimeout(() => onSuccess?.(), 3000);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function handlePasskeySkip() {
|
|
152
|
+
fetch(`${apiBase}/skip-passkey`, { method: 'POST' });
|
|
153
|
+
onSuccess?.();
|
|
154
|
+
}
|
|
155
|
+
</script>
|
|
156
|
+
|
|
157
|
+
<div class="anahtar-auth">
|
|
158
|
+
{#if step === 1}
|
|
159
|
+
<form
|
|
160
|
+
onsubmit={(e) => {
|
|
161
|
+
e.preventDefault();
|
|
162
|
+
handleEmailSubmit();
|
|
163
|
+
}}
|
|
164
|
+
class="anahtar-auth-form"
|
|
165
|
+
>
|
|
166
|
+
<input
|
|
167
|
+
type="email"
|
|
168
|
+
bind:value={email}
|
|
169
|
+
required
|
|
170
|
+
autocomplete="username webauthn"
|
|
171
|
+
placeholder="you@example.com"
|
|
172
|
+
class="anahtar-input"
|
|
173
|
+
/>
|
|
174
|
+
|
|
175
|
+
{#if error}
|
|
176
|
+
<p class="anahtar-error">{error}</p>
|
|
177
|
+
{/if}
|
|
178
|
+
|
|
179
|
+
<button type="submit" disabled={loading} class="anahtar-button">
|
|
180
|
+
{loading ? '...' : 'Continue'}
|
|
181
|
+
</button>
|
|
182
|
+
</form>
|
|
183
|
+
{:else if step === 2}
|
|
184
|
+
<div class="anahtar-otp-step">
|
|
185
|
+
<p class="anahtar-subtitle">We sent a code to</p>
|
|
186
|
+
<p class="anahtar-email">{email}</p>
|
|
187
|
+
|
|
188
|
+
<OtpInput bind:this={otpInput} onComplete={handleOtpComplete} disabled={loading} />
|
|
189
|
+
|
|
190
|
+
{#if error}
|
|
191
|
+
<p class="anahtar-error">{error}</p>
|
|
192
|
+
{/if}
|
|
193
|
+
|
|
194
|
+
{#if loading}
|
|
195
|
+
<p class="anahtar-subtitle">Verifying...</p>
|
|
196
|
+
{/if}
|
|
197
|
+
|
|
198
|
+
<div class="anahtar-links">
|
|
199
|
+
<button onclick={resendCode} disabled={loading} class="anahtar-link">
|
|
200
|
+
Didn't get it? Resend
|
|
201
|
+
</button>
|
|
202
|
+
<button
|
|
203
|
+
onclick={() => {
|
|
204
|
+
step = 1;
|
|
205
|
+
error = '';
|
|
206
|
+
}}
|
|
207
|
+
class="anahtar-link"
|
|
208
|
+
>
|
|
209
|
+
Use a different email
|
|
210
|
+
</button>
|
|
211
|
+
</div>
|
|
212
|
+
</div>
|
|
213
|
+
{:else if step === 3}
|
|
214
|
+
<PasskeyPrompt onRegister={handlePasskeyRegister} onSkip={handlePasskeySkip} />
|
|
215
|
+
{:else if step === 4}
|
|
216
|
+
<div class="anahtar-congrats">
|
|
217
|
+
<div class="anahtar-congrats-icon">
|
|
218
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
|
|
219
|
+
<circle cx="7.5" cy="15.5" r="5.5"/>
|
|
220
|
+
<path d="m11.5 12 4-4"/>
|
|
221
|
+
<path d="m15 7 2 2"/>
|
|
222
|
+
<path d="m17.5 4.5 2 2"/>
|
|
223
|
+
</svg>
|
|
224
|
+
</div>
|
|
225
|
+
<p class="anahtar-congrats-title">You're a passkey!</p>
|
|
226
|
+
<button onclick={() => onSuccess?.()} class="anahtar-button">
|
|
227
|
+
Continue
|
|
228
|
+
</button>
|
|
229
|
+
</div>
|
|
230
|
+
{/if}
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
<style>
|
|
234
|
+
.anahtar-auth {
|
|
235
|
+
width: 100%;
|
|
236
|
+
max-width: 24rem;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.anahtar-auth-form {
|
|
240
|
+
display: flex;
|
|
241
|
+
flex-direction: column;
|
|
242
|
+
gap: 1rem;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.anahtar-input {
|
|
246
|
+
width: 100%;
|
|
247
|
+
padding: 0.5rem 0.75rem;
|
|
248
|
+
font-size: 0.875rem;
|
|
249
|
+
border: 1px solid var(--anahtar-border, #d1d5db);
|
|
250
|
+
border-radius: 0.375rem;
|
|
251
|
+
background: var(--anahtar-bg, transparent);
|
|
252
|
+
color: var(--anahtar-fg, inherit);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
.anahtar-input:focus {
|
|
256
|
+
outline: none;
|
|
257
|
+
box-shadow: 0 0 0 2px var(--anahtar-ring, #3b82f6);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
.anahtar-button {
|
|
261
|
+
width: 100%;
|
|
262
|
+
padding: 0.5rem;
|
|
263
|
+
font-size: 0.875rem;
|
|
264
|
+
font-weight: 500;
|
|
265
|
+
border-radius: 0.375rem;
|
|
266
|
+
background: var(--anahtar-primary, #3b82f6);
|
|
267
|
+
color: var(--anahtar-primary-fg, #fff);
|
|
268
|
+
border: none;
|
|
269
|
+
cursor: pointer;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.anahtar-button:hover {
|
|
273
|
+
opacity: 0.9;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
.anahtar-button:disabled {
|
|
277
|
+
opacity: 0.5;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
.anahtar-otp-step {
|
|
281
|
+
display: flex;
|
|
282
|
+
flex-direction: column;
|
|
283
|
+
align-items: center;
|
|
284
|
+
gap: 0.5rem;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
.anahtar-subtitle {
|
|
288
|
+
font-size: 0.875rem;
|
|
289
|
+
opacity: 0.6;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
.anahtar-email {
|
|
293
|
+
font-size: 0.875rem;
|
|
294
|
+
font-weight: 500;
|
|
295
|
+
margin-bottom: 0.5rem;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
.anahtar-error {
|
|
299
|
+
font-size: 0.875rem;
|
|
300
|
+
color: var(--anahtar-error, #ef4444);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.anahtar-links {
|
|
304
|
+
display: flex;
|
|
305
|
+
flex-direction: column;
|
|
306
|
+
align-items: center;
|
|
307
|
+
gap: 0.5rem;
|
|
308
|
+
margin-top: 0.5rem;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
.anahtar-link {
|
|
312
|
+
font-size: 0.875rem;
|
|
313
|
+
opacity: 0.6;
|
|
314
|
+
background: none;
|
|
315
|
+
border: none;
|
|
316
|
+
cursor: pointer;
|
|
317
|
+
color: inherit;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.anahtar-link:hover {
|
|
321
|
+
opacity: 1;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.anahtar-link:disabled {
|
|
325
|
+
opacity: 0.3;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
.anahtar-congrats {
|
|
329
|
+
display: flex;
|
|
330
|
+
flex-direction: column;
|
|
331
|
+
align-items: center;
|
|
332
|
+
gap: 1rem;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
.anahtar-congrats-icon {
|
|
336
|
+
width: 4rem;
|
|
337
|
+
height: 4rem;
|
|
338
|
+
border-radius: 50%;
|
|
339
|
+
background: var(--anahtar-primary, #3b82f6);
|
|
340
|
+
color: var(--anahtar-primary-fg, #fff);
|
|
341
|
+
display: flex;
|
|
342
|
+
align-items: center;
|
|
343
|
+
justify-content: center;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
.anahtar-congrats-title {
|
|
347
|
+
font-size: 1.125rem;
|
|
348
|
+
font-weight: 600;
|
|
349
|
+
margin-bottom: 0.5rem;
|
|
350
|
+
}
|
|
351
|
+
</style>
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
interface Props {
|
|
3
|
+
length?: number;
|
|
4
|
+
onComplete?: (code: string) => void;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let { length = 5, onComplete, disabled = false }: Props = $props();
|
|
9
|
+
|
|
10
|
+
let digits = $state<string[]>(Array(length).fill(''));
|
|
11
|
+
let inputs = $state<HTMLInputElement[]>([]);
|
|
12
|
+
|
|
13
|
+
export function clear() {
|
|
14
|
+
digits = Array(length).fill('');
|
|
15
|
+
inputs[0]?.focus();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function focus() {
|
|
19
|
+
inputs[0]?.focus();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function handleInput(index: number, e: Event) {
|
|
23
|
+
const input = e.target as HTMLInputElement;
|
|
24
|
+
const val = input.value.replace(/\D/g, '');
|
|
25
|
+
digits[index] = val.slice(0, 1);
|
|
26
|
+
input.value = digits[index];
|
|
27
|
+
|
|
28
|
+
if (val && index < length - 1) {
|
|
29
|
+
inputs[index + 1]?.focus();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (digits.every((d) => d.length === 1)) {
|
|
33
|
+
onComplete?.(digits.join(''));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function handleKeydown(index: number, e: KeyboardEvent) {
|
|
38
|
+
if (e.key === 'Backspace' && !digits[index] && index > 0) {
|
|
39
|
+
inputs[index - 1]?.focus();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function handlePaste(e: ClipboardEvent) {
|
|
44
|
+
e.preventDefault();
|
|
45
|
+
const pasted = (e.clipboardData?.getData('text') ?? '').replace(/\D/g, '');
|
|
46
|
+
for (let i = 0; i < length; i++) {
|
|
47
|
+
digits[i] = pasted[i] ?? '';
|
|
48
|
+
}
|
|
49
|
+
const nextEmpty = digits.findIndex((d) => !d);
|
|
50
|
+
const focusIdx = nextEmpty === -1 ? length - 1 : nextEmpty;
|
|
51
|
+
inputs[focusIdx]?.focus();
|
|
52
|
+
|
|
53
|
+
if (digits.every((d) => d.length === 1)) {
|
|
54
|
+
onComplete?.(digits.join(''));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<div class="anahtar-otp">
|
|
60
|
+
{#each digits as _, i}
|
|
61
|
+
<input
|
|
62
|
+
bind:this={inputs[i]}
|
|
63
|
+
type="text"
|
|
64
|
+
maxlength="1"
|
|
65
|
+
inputmode="numeric"
|
|
66
|
+
value={digits[i]}
|
|
67
|
+
{disabled}
|
|
68
|
+
oninput={(e) => handleInput(i, e)}
|
|
69
|
+
onkeydown={(e) => handleKeydown(i, e)}
|
|
70
|
+
onpaste={handlePaste}
|
|
71
|
+
class="anahtar-otp-digit"
|
|
72
|
+
/>
|
|
73
|
+
{/each}
|
|
74
|
+
</div>
|
|
75
|
+
|
|
76
|
+
<style>
|
|
77
|
+
.anahtar-otp {
|
|
78
|
+
display: flex;
|
|
79
|
+
justify-content: center;
|
|
80
|
+
gap: 0.5rem;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.anahtar-otp-digit {
|
|
84
|
+
width: 2.75rem;
|
|
85
|
+
height: 3rem;
|
|
86
|
+
text-align: center;
|
|
87
|
+
font-size: 1.125rem;
|
|
88
|
+
border: 1px solid var(--anahtar-border, #d1d5db);
|
|
89
|
+
border-radius: 0.375rem;
|
|
90
|
+
background: var(--anahtar-bg, transparent);
|
|
91
|
+
color: var(--anahtar-fg, inherit);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.anahtar-otp-digit:focus {
|
|
95
|
+
outline: none;
|
|
96
|
+
box-shadow: 0 0 0 2px var(--anahtar-ring, #3b82f6);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.anahtar-otp-digit:disabled {
|
|
100
|
+
opacity: 0.5;
|
|
101
|
+
}
|
|
102
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
length?: number;
|
|
3
|
+
onComplete?: (code: string) => void;
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare const OtpInput: import("svelte").Component<Props, {
|
|
7
|
+
clear: () => void;
|
|
8
|
+
focus: () => void;
|
|
9
|
+
}, "">;
|
|
10
|
+
type OtpInput = ReturnType<typeof OtpInput>;
|
|
11
|
+
export default OtpInput;
|