@stacksjs/components 0.2.913 → 0.2.915

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.
@@ -0,0 +1,78 @@
1
+ <script server>
2
+ /*
3
+ * <BiometricSignIn>: the "Sign in with Face ID" button on a sign-in screen.
4
+ *
5
+ * <BiometricSignIn reason="Sign in to HQ"
6
+ * @signin="session.exchange($event.token)"
7
+ * @fallback="showPassword()" />
8
+ *
9
+ * It is offered only when this device can release a kept credential: inside
10
+ * Craft, with Face ID or Touch ID enrolled, after the app kept one with
11
+ * `createBiometricSignIn().enable(token)` at its last password sign-in. In a
12
+ * browser or on Android it renders nothing, so the same screen serves every
13
+ * platform.
14
+ *
15
+ * Pressing it shows the system prompt (`reason` is the line under it). It
16
+ * emits:
17
+ * - `signin` with `{ token }` on a match;
18
+ * - `fallback` with `{ reason: 'cancelled' | 'invalidated' }` when the person
19
+ * dismissed the prompt, or nothing came back because another face or
20
+ * finger was enrolled (the credential is forgotten, the button goes away);
21
+ * - `failed` with `{ code, message }` when the face or finger did not match
22
+ * or the host refused. (Not `error`: an emitted event bubbles, and an
23
+ * `error` reaching window would be logged by an error tracker as a crash.)
24
+ *
25
+ * Keep a device token the server can revoke, never the password. The flow
26
+ * lives in ./biometric (`createBiometricSignIn`), which an app's settings
27
+ * screen uses for "Turn on Face ID" and sign-out.
28
+ */
29
+ export const credentialKey = $props.credentialKey || 'session'
30
+ export const reason = $props.reason || ''
31
+ export const variant = $props.variant || 'secondary'
32
+ export const fullWidth = $bool($props.fullWidth, true)
33
+ export const className = $props.className || ''
34
+ </script>
35
+
36
+ <script client>
37
+ import { biometricSignInLabel, createBiometricSignIn } from './biometric'
38
+
39
+ const emit = defineEmits(['signin', 'fallback', 'failed'])
40
+ const flow = createBiometricSignIn({{ credentialKey }})
41
+ const offered = state(false)
42
+ const label = state('')
43
+ const busy = state(false)
44
+
45
+ async function look() {
46
+ const found = await flow.probe()
47
+ label.set(found.available ? biometricSignInLabel(found.kind) : '')
48
+ offered.set(found.available && found.saved)
49
+ }
50
+
51
+ async function press() {
52
+ if (busy())
53
+ return
54
+ busy.set(true)
55
+ try {
56
+ const result = await flow.signIn({{ reason }})
57
+ if (result.status === 'signed-in')
58
+ emit('signin', { token: result.token })
59
+ else if (result.status === 'failed')
60
+ emit('failed', { code: result.code, message: result.message })
61
+ else
62
+ emit('fallback', { reason: result.status })
63
+ if (result.status === 'invalidated')
64
+ offered.set(false)
65
+ }
66
+ finally {
67
+ busy.set(false)
68
+ }
69
+ }
70
+
71
+ onMount(look)
72
+
73
+ defineExpose({ refresh: look, offered })
74
+ </script>
75
+
76
+ <div :show="offered" class="{{ className }}" data-biometric-sign-in>
77
+ <Button variant="{{ variant }}" fullWidth="{{ fullWidth }}" :label="label()" :loading="busy()" @click="press()" />
78
+ </div>
@@ -0,0 +1,94 @@
1
+ // src/ui/auth/biometric.ts
2
+ var BIOMETRIC_DEFAULT_KEY = "session";
3
+ var LABELS = {
4
+ faceId: "Face ID",
5
+ touchId: "Touch ID",
6
+ fingerprint: "Fingerprint"
7
+ };
8
+ function biometricLabel(kind) {
9
+ return typeof kind === "string" ? LABELS[kind] || "" : "";
10
+ }
11
+ function biometricSignInLabel(kind) {
12
+ const label = biometricLabel(kind);
13
+ return label ? `Sign in with ${label}` : "";
14
+ }
15
+ function biometricHost() {
16
+ const craft = globalThis.craft;
17
+ const host = craft?.biometrics;
18
+ if (!host)
19
+ return null;
20
+ const needed = ["isAvailable", "getBiometricType", "storeCredential", "getCredential", "hasCredential", "deleteCredential"];
21
+ return needed.every((name) => typeof host[name] === "function") ? host : null;
22
+ }
23
+ var NOTHING = Object.freeze({ available: false, kind: null, label: "", saved: false });
24
+ function codeOf(error) {
25
+ const code = error?.code;
26
+ return typeof code === "string" && code ? code : "UNKNOWN";
27
+ }
28
+ function messageOf(error) {
29
+ return error instanceof Error ? error.message : String(error ?? "");
30
+ }
31
+ function createBiometricSignIn(key = BIOMETRIC_DEFAULT_KEY, host) {
32
+ const name = String(key || "").trim() || BIOMETRIC_DEFAULT_KEY;
33
+ const current = () => host === undefined ? biometricHost() : host;
34
+ async function probe() {
35
+ const bio = current();
36
+ if (!bio)
37
+ return NOTHING;
38
+ try {
39
+ if (!await bio.isAvailable())
40
+ return NOTHING;
41
+ const kind = await bio.getBiometricType();
42
+ const label = biometricLabel(kind);
43
+ if (!label)
44
+ return NOTHING;
45
+ return { available: true, kind, label, saved: await bio.hasCredential(name) === true };
46
+ } catch {
47
+ return NOTHING;
48
+ }
49
+ }
50
+ async function signIn(reason) {
51
+ const bio = current();
52
+ if (!bio)
53
+ return { status: "failed", code: "UNSUPPORTED", message: "Biometric sign-in is not available here." };
54
+ try {
55
+ const token = await bio.getCredential(name, reason || undefined);
56
+ if (typeof token === "string" && token)
57
+ return { status: "signed-in", token };
58
+ await bio.deleteCredential(name).catch(() => {});
59
+ return { status: "invalidated" };
60
+ } catch (error) {
61
+ const code = codeOf(error);
62
+ if (code === "AUTHENTICATION_CANCELLED")
63
+ return { status: "cancelled" };
64
+ return { status: "failed", code, message: messageOf(error) };
65
+ }
66
+ }
67
+ async function enable(token) {
68
+ const bio = current();
69
+ if (!bio || typeof token !== "string" || !token)
70
+ return false;
71
+ try {
72
+ if (!await bio.isAvailable())
73
+ return false;
74
+ await bio.storeCredential(name, token);
75
+ return true;
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
80
+ async function forget() {
81
+ const bio = current();
82
+ if (!bio)
83
+ return;
84
+ await bio.deleteCredential(name).catch(() => {});
85
+ }
86
+ return { probe, signIn, enable, forget };
87
+ }
88
+ export {
89
+ BIOMETRIC_DEFAULT_KEY,
90
+ biometricHost,
91
+ biometricLabel,
92
+ biometricSignInLabel,
93
+ createBiometricSignIn
94
+ };