@devopsplaybook.io/common-web 0.13.0-beta.24.c1e57ae → 0.14.0-beta.25.1e6c035
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/CHANGELOG.md +23 -0
- package/README.md +179 -0
- package/components/AuthorizeDeviceCard.vue +575 -0
- package/components/LoginCodeInput.vue +141 -0
- package/components/LoginWithCodeCard.vue +408 -0
- package/composables/useLoginCode.ts +436 -0
- package/package.json +1 -1
- package/services/LoginCodeService.ts +460 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<section class="login-with-code-card" :aria-labelledby="titleId">
|
|
3
|
+
<h2 :id="titleId" class="login-with-code-title">{{ copy.title }}</h2>
|
|
4
|
+
<p class="login-with-code-instructions">{{ copy.instructions }}</p>
|
|
5
|
+
|
|
6
|
+
<template v-if="!unavailable">
|
|
7
|
+
<div v-if="showCode" class="login-with-code-display">
|
|
8
|
+
<p class="login-with-code-label" aria-hidden="true">
|
|
9
|
+
{{ copy.codeLabel }}
|
|
10
|
+
</p>
|
|
11
|
+
<code class="login-with-code-value" aria-hidden="true">
|
|
12
|
+
{{ loginCode.formattedCode.value }}
|
|
13
|
+
</code>
|
|
14
|
+
<span class="login-with-code-sr-only">{{ spelledCode }}</span>
|
|
15
|
+
<button
|
|
16
|
+
type="button"
|
|
17
|
+
class="login-with-code-copy"
|
|
18
|
+
@click="copyCode"
|
|
19
|
+
>
|
|
20
|
+
{{ copied ? copy.copied : copy.copy }}
|
|
21
|
+
</button>
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
<p v-if="showCountdowns" class="login-with-code-countdown">
|
|
25
|
+
<span v-if="refreshText">{{ refreshText }}</span>
|
|
26
|
+
<span v-if="refreshText && expiryText" aria-hidden="true"> · </span>
|
|
27
|
+
<span v-if="expiryText">{{ expiryText }}</span>
|
|
28
|
+
</p>
|
|
29
|
+
</template>
|
|
30
|
+
|
|
31
|
+
<p
|
|
32
|
+
:id="statusId"
|
|
33
|
+
class="login-with-code-status"
|
|
34
|
+
role="status"
|
|
35
|
+
aria-live="polite"
|
|
36
|
+
aria-atomic="true"
|
|
37
|
+
>
|
|
38
|
+
{{ statusText }}
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<div class="login-with-code-actions">
|
|
42
|
+
<button
|
|
43
|
+
v-if="canCancel"
|
|
44
|
+
type="button"
|
|
45
|
+
class="login-with-code-button login-with-code-button-secondary"
|
|
46
|
+
@click="handleCancel"
|
|
47
|
+
>
|
|
48
|
+
{{ copy.cancel }}
|
|
49
|
+
</button>
|
|
50
|
+
<button
|
|
51
|
+
v-if="canRetry"
|
|
52
|
+
type="button"
|
|
53
|
+
class="login-with-code-button login-with-code-button-primary"
|
|
54
|
+
@click="handleRetry"
|
|
55
|
+
>
|
|
56
|
+
{{ copy.retry }}
|
|
57
|
+
</button>
|
|
58
|
+
<button
|
|
59
|
+
type="button"
|
|
60
|
+
class="login-with-code-button login-with-code-button-link"
|
|
61
|
+
@click="emitUsePassword"
|
|
62
|
+
>
|
|
63
|
+
{{ copy.usePassword }}
|
|
64
|
+
</button>
|
|
65
|
+
</div>
|
|
66
|
+
</section>
|
|
67
|
+
</template>
|
|
68
|
+
|
|
69
|
+
<script setup lang="ts">
|
|
70
|
+
import { computed, onMounted, onUnmounted, ref, useId } from "vue";
|
|
71
|
+
import { useLoginCode } from "../composables/useLoginCode";
|
|
72
|
+
import type { LoginCodeService } from "../services/LoginCodeService";
|
|
73
|
+
|
|
74
|
+
interface LoginWithCodeCardLabels {
|
|
75
|
+
title: string;
|
|
76
|
+
instructions: string;
|
|
77
|
+
codeLabel: string;
|
|
78
|
+
preparing: string;
|
|
79
|
+
waiting: string;
|
|
80
|
+
approved: string;
|
|
81
|
+
expired: string;
|
|
82
|
+
cancelled: string;
|
|
83
|
+
error: string;
|
|
84
|
+
unavailable: string;
|
|
85
|
+
refreshIn: string;
|
|
86
|
+
expiresIn: string;
|
|
87
|
+
copy: string;
|
|
88
|
+
copied: string;
|
|
89
|
+
cancel: string;
|
|
90
|
+
usePassword: string;
|
|
91
|
+
retry: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const DEFAULT_LABELS: LoginWithCodeCardLabels = {
|
|
95
|
+
title: "Sign in with a code",
|
|
96
|
+
instructions:
|
|
97
|
+
"Enter this code on a device where you are already signed in, or scan the flow from that device.",
|
|
98
|
+
codeLabel: "Login code",
|
|
99
|
+
preparing: "Preparing a code…",
|
|
100
|
+
waiting: "Waiting for approval",
|
|
101
|
+
approved: "Approved",
|
|
102
|
+
expired: "Code expired",
|
|
103
|
+
cancelled: "Login cancelled",
|
|
104
|
+
error: "Something went wrong",
|
|
105
|
+
unavailable: "Login with a code is unavailable. Use your password instead.",
|
|
106
|
+
refreshIn: "New code in {seconds}s",
|
|
107
|
+
expiresIn: "Expires in {seconds}s",
|
|
108
|
+
copy: "Copy code",
|
|
109
|
+
copied: "Copied",
|
|
110
|
+
cancel: "Cancel",
|
|
111
|
+
usePassword: "Use password instead",
|
|
112
|
+
retry: "Try again",
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const props = withDefaults(
|
|
116
|
+
defineProps<{
|
|
117
|
+
service?: LoginCodeService;
|
|
118
|
+
onSession?: (token: string, user: unknown) => void | Promise<void>;
|
|
119
|
+
autoStart?: boolean;
|
|
120
|
+
labels?: Partial<LoginWithCodeCardLabels>;
|
|
121
|
+
}>(),
|
|
122
|
+
{
|
|
123
|
+
service: undefined,
|
|
124
|
+
onSession: undefined,
|
|
125
|
+
autoStart: true,
|
|
126
|
+
labels: undefined,
|
|
127
|
+
},
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const emit = defineEmits<{
|
|
131
|
+
approved: [payload: { token: string; user: unknown }];
|
|
132
|
+
cancel: [];
|
|
133
|
+
"use-password": [];
|
|
134
|
+
}>();
|
|
135
|
+
|
|
136
|
+
const copy = computed<LoginWithCodeCardLabels>(() => ({
|
|
137
|
+
...DEFAULT_LABELS,
|
|
138
|
+
...props.labels,
|
|
139
|
+
}));
|
|
140
|
+
|
|
141
|
+
const loginCode = useLoginCode({
|
|
142
|
+
service: props.service,
|
|
143
|
+
onSession: handleSession,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const uid = useId();
|
|
147
|
+
const titleId = `login-with-code-title-${uid}`;
|
|
148
|
+
const statusId = `login-with-code-status-${uid}`;
|
|
149
|
+
|
|
150
|
+
const copied = ref(false);
|
|
151
|
+
let copyTimer: ReturnType<typeof setTimeout> | null = null;
|
|
152
|
+
|
|
153
|
+
const unavailable = computed(() => {
|
|
154
|
+
if (loginCode.error.value?.code === "LOGIN_CODE_UNAVAILABLE") return true;
|
|
155
|
+
const capabilities = loginCode.capabilities.value;
|
|
156
|
+
return (
|
|
157
|
+
capabilities !== null && (!capabilities.enabled || !capabilities.available)
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const showCode = computed(
|
|
162
|
+
() => !unavailable.value && loginCode.code.value !== null,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const showCountdowns = computed(
|
|
166
|
+
() => loginCode.status.value === "waiting" && !unavailable.value,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const canCancel = computed(
|
|
170
|
+
() =>
|
|
171
|
+
loginCode.status.value === "creating" ||
|
|
172
|
+
loginCode.status.value === "waiting",
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const canRetry = computed(() => {
|
|
176
|
+
const status = loginCode.status.value;
|
|
177
|
+
return (
|
|
178
|
+
!unavailable.value &&
|
|
179
|
+
(status === "error" || status === "expired" || status === "cancelled")
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const statusText = computed(() => {
|
|
184
|
+
if (unavailable.value) return copy.value.unavailable;
|
|
185
|
+
switch (loginCode.status.value) {
|
|
186
|
+
case "approved":
|
|
187
|
+
return copy.value.approved;
|
|
188
|
+
case "expired":
|
|
189
|
+
return copy.value.expired;
|
|
190
|
+
case "cancelled":
|
|
191
|
+
return copy.value.cancelled;
|
|
192
|
+
case "error":
|
|
193
|
+
return copy.value.error;
|
|
194
|
+
case "waiting":
|
|
195
|
+
return copy.value.waiting;
|
|
196
|
+
default:
|
|
197
|
+
return copy.value.preparing;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const refreshText = computed(() =>
|
|
202
|
+
formatSeconds(copy.value.refreshIn, loginCode.secondsUntilRefresh.value),
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const expiryText = computed(() =>
|
|
206
|
+
formatSeconds(copy.value.expiresIn, loginCode.secondsUntilExpiry.value),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
const spelledCode = computed(() => {
|
|
210
|
+
const value = loginCode.code.value;
|
|
211
|
+
if (!value) return "";
|
|
212
|
+
return `${copy.value.codeLabel}: ${value.split("").join(" ")}`;
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
function formatSeconds(template: string, seconds: number | null): string | null {
|
|
216
|
+
if (seconds === null) return null;
|
|
217
|
+
return template.replace("{seconds}", String(seconds));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function handleSession(token: string, user: unknown): Promise<void> {
|
|
221
|
+
try {
|
|
222
|
+
await props.onSession?.(token, user);
|
|
223
|
+
} finally {
|
|
224
|
+
emit("approved", { token, user });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function copyCode(): Promise<void> {
|
|
229
|
+
const value = loginCode.code.value;
|
|
230
|
+
if (!value) return;
|
|
231
|
+
const clipboard = typeof navigator === "undefined" ? undefined : navigator.clipboard;
|
|
232
|
+
if (!clipboard?.writeText) return;
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
await clipboard.writeText(value);
|
|
236
|
+
} catch {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
copied.value = true;
|
|
240
|
+
if (copyTimer !== null) clearTimeout(copyTimer);
|
|
241
|
+
copyTimer = setTimeout(() => {
|
|
242
|
+
copyTimer = null;
|
|
243
|
+
copied.value = false;
|
|
244
|
+
}, 2000);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function handleCancel(): Promise<void> {
|
|
248
|
+
await loginCode.cancel();
|
|
249
|
+
emit("cancel");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function handleRetry(): void {
|
|
253
|
+
void loginCode.retry();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function emitUsePassword(): void {
|
|
257
|
+
emit("use-password");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
onMounted(() => {
|
|
261
|
+
if (props.autoStart) void loginCode.start();
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
onUnmounted(() => {
|
|
265
|
+
if (copyTimer !== null) {
|
|
266
|
+
clearTimeout(copyTimer);
|
|
267
|
+
copyTimer = null;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
</script>
|
|
271
|
+
|
|
272
|
+
<style scoped>
|
|
273
|
+
.login-with-code-card {
|
|
274
|
+
max-width: 30rem;
|
|
275
|
+
padding: clamp(var(--space-base, 1rem), 3vw, var(--space-xl, 2rem));
|
|
276
|
+
color: var(--color-text, #171717);
|
|
277
|
+
background: var(--color-surface, #fff);
|
|
278
|
+
border: 1px solid var(--color-border, #d6d6d6);
|
|
279
|
+
border-radius: var(--radius-lg, 0.5rem);
|
|
280
|
+
box-shadow: var(--shadow-sm, 0 1px 2px rgb(0 0 0 / 10%));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.login-with-code-title {
|
|
284
|
+
margin: 0 0 var(--space-sm, 0.5rem);
|
|
285
|
+
font-size: var(--text-xl, 1.25rem);
|
|
286
|
+
line-height: 1.3;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.login-with-code-instructions {
|
|
290
|
+
margin: 0 0 var(--space-base, 1rem);
|
|
291
|
+
color: var(--color-text-muted, #5f6368);
|
|
292
|
+
font-size: var(--text-sm, 0.875rem);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
.login-with-code-display {
|
|
296
|
+
display: flex;
|
|
297
|
+
flex-direction: column;
|
|
298
|
+
align-items: center;
|
|
299
|
+
gap: var(--space-sm, 0.5rem);
|
|
300
|
+
padding: var(--space-base, 1rem);
|
|
301
|
+
margin-bottom: var(--space-base, 1rem);
|
|
302
|
+
background: var(--color-surface-raised, #f5f5f5);
|
|
303
|
+
border-radius: var(--radius-md, 0.375rem);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
.login-with-code-label {
|
|
307
|
+
margin: 0;
|
|
308
|
+
color: var(--color-text-muted, #5f6368);
|
|
309
|
+
font-size: var(--text-sm, 0.875rem);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
.login-with-code-value {
|
|
313
|
+
color: var(--color-text, #171717);
|
|
314
|
+
font-family: ui-monospace, "SFMono-Regular", "Consolas", monospace;
|
|
315
|
+
font-size: 2.5rem;
|
|
316
|
+
font-weight: 700;
|
|
317
|
+
letter-spacing: 0.15em;
|
|
318
|
+
line-height: 1.2;
|
|
319
|
+
white-space: nowrap;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
.login-with-code-sr-only {
|
|
323
|
+
position: absolute;
|
|
324
|
+
width: 1px;
|
|
325
|
+
height: 1px;
|
|
326
|
+
padding: 0;
|
|
327
|
+
margin: -1px;
|
|
328
|
+
overflow: hidden;
|
|
329
|
+
clip: rect(0 0 0 0);
|
|
330
|
+
white-space: nowrap;
|
|
331
|
+
border: 0;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
.login-with-code-copy {
|
|
335
|
+
padding: var(--space-xs, 0.25rem) var(--space-base, 1rem);
|
|
336
|
+
color: var(--color-text, #171717);
|
|
337
|
+
font: inherit;
|
|
338
|
+
font-size: var(--text-sm, 0.875rem);
|
|
339
|
+
background: var(--color-surface, #fff);
|
|
340
|
+
border: 1px solid var(--color-border, #d6d6d6);
|
|
341
|
+
border-radius: var(--radius-sm, 0.25rem);
|
|
342
|
+
cursor: pointer;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
.login-with-code-copy:focus-visible {
|
|
346
|
+
outline: 3px solid var(--color-primary, #c62828);
|
|
347
|
+
outline-offset: 2px;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
.login-with-code-countdown {
|
|
351
|
+
margin: 0 0 var(--space-base, 1rem);
|
|
352
|
+
color: var(--color-text-muted, #5f6368);
|
|
353
|
+
font-size: var(--text-sm, 0.875rem);
|
|
354
|
+
text-align: center;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.login-with-code-status {
|
|
358
|
+
margin: 0 0 var(--space-base, 1rem);
|
|
359
|
+
color: var(--color-text, #171717);
|
|
360
|
+
font-weight: 600;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
.login-with-code-actions {
|
|
364
|
+
display: flex;
|
|
365
|
+
flex-wrap: wrap;
|
|
366
|
+
gap: var(--space-sm, 0.5rem);
|
|
367
|
+
align-items: center;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
.login-with-code-button {
|
|
371
|
+
padding: var(--space-sm, 0.5rem) var(--space-base, 1rem);
|
|
372
|
+
font: inherit;
|
|
373
|
+
font-size: var(--text-sm, 0.875rem);
|
|
374
|
+
border-radius: var(--radius-md, 0.375rem);
|
|
375
|
+
cursor: pointer;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
.login-with-code-button:focus-visible {
|
|
379
|
+
outline: 3px solid var(--color-primary, #c62828);
|
|
380
|
+
outline-offset: 2px;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
.login-with-code-button-primary {
|
|
384
|
+
color: #fff;
|
|
385
|
+
background: var(--color-primary, #c62828);
|
|
386
|
+
border: 1px solid var(--color-primary, #c62828);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
.login-with-code-button-primary:hover {
|
|
390
|
+
background: var(--color-primary-hover, #a61f1f);
|
|
391
|
+
border-color: var(--color-primary-hover, #a61f1f);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
.login-with-code-button-secondary {
|
|
395
|
+
color: var(--color-text, #171717);
|
|
396
|
+
background: var(--color-surface, #fff);
|
|
397
|
+
border: 1px solid var(--color-text-muted, #5f6368);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
.login-with-code-button-link {
|
|
401
|
+
padding-right: var(--space-xs, 0.25rem);
|
|
402
|
+
padding-left: var(--space-xs, 0.25rem);
|
|
403
|
+
color: var(--color-primary, #c62828);
|
|
404
|
+
background: transparent;
|
|
405
|
+
border: 1px solid transparent;
|
|
406
|
+
text-decoration: underline;
|
|
407
|
+
}
|
|
408
|
+
</style>
|