add mp with icons and update after review
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { Injectable, inject, signal } from '@angular/core';
|
||||
|
||||
import type { AuthIntent, AuthPromptResult } from './auth.models';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthPromptService {
|
||||
private readonly auth = inject(AuthService);
|
||||
|
||||
readonly visible = signal(false);
|
||||
readonly intent = signal<AuthIntent>('collection');
|
||||
|
||||
private resolver: ((result: AuthPromptResult) => void) | null = null;
|
||||
|
||||
prompt(intent: AuthIntent): Promise<AuthPromptResult> {
|
||||
if (this.auth.authenticated()) {
|
||||
return Promise.resolve('authenticated');
|
||||
}
|
||||
|
||||
if (this.visible() && this.resolver) {
|
||||
this.intent.set(intent);
|
||||
return new Promise((resolve) => {
|
||||
const previous = this.resolver;
|
||||
this.resolver = (result) => {
|
||||
previous?.(result);
|
||||
resolve(result);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
this.intent.set(intent);
|
||||
this.visible.set(true);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
this.resolver = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
dismiss(): void {
|
||||
this.close('dismissed');
|
||||
}
|
||||
|
||||
finish(): void {
|
||||
this.close('authenticated');
|
||||
}
|
||||
|
||||
private close(result: AuthPromptResult): void {
|
||||
this.visible.set(false);
|
||||
this.resolver?.(result);
|
||||
this.resolver = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export type AuthIntent = 'claim' | 'collection';
|
||||
|
||||
export type AuthPromptResult = 'authenticated' | 'dismissed';
|
||||
|
||||
export type AuthStep = 'email' | 'code';
|
||||
|
||||
export interface AuthSession {
|
||||
email: string;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { Injectable, signal } from '@angular/core';
|
||||
|
||||
import type { AuthSession, AuthStep } from './auth.models';
|
||||
|
||||
const SESSION_KEY = 'gorodar.session';
|
||||
const RESEND_SECONDS = 60;
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
readonly email = signal('');
|
||||
readonly authenticated = signal(false);
|
||||
readonly step = signal<AuthStep>('email');
|
||||
readonly resendSeconds = signal(0);
|
||||
|
||||
private timerId: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
constructor() {
|
||||
const session = this.readSession();
|
||||
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.email.set(session.email);
|
||||
this.authenticated.set(true);
|
||||
}
|
||||
|
||||
requestCode(email: string): boolean {
|
||||
const normalized = email.trim();
|
||||
|
||||
if (!this.isEmail(normalized)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.email.set(normalized);
|
||||
this.step.set('code');
|
||||
this.startTimer();
|
||||
return true;
|
||||
}
|
||||
|
||||
confirmCode(code: string): boolean {
|
||||
if (!/^\d{6}$/.test(code) || !this.email()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.authenticated.set(true);
|
||||
this.step.set('email');
|
||||
this.clearTimer();
|
||||
this.writeSession({ email: this.email() });
|
||||
return true;
|
||||
}
|
||||
|
||||
changeEmail(): void {
|
||||
this.clearTimer();
|
||||
this.step.set('email');
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
this.authenticated.set(false);
|
||||
this.step.set('email');
|
||||
this.clearTimer();
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
}
|
||||
|
||||
private startTimer(): void {
|
||||
this.clearTimer();
|
||||
this.resendSeconds.set(RESEND_SECONDS);
|
||||
this.timerId = setInterval(() => {
|
||||
const next = this.resendSeconds() - 1;
|
||||
this.resendSeconds.set(Math.max(0, next));
|
||||
|
||||
if (next <= 0) {
|
||||
this.clearTimer();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
private clearTimer(): void {
|
||||
if (this.timerId !== null) {
|
||||
clearInterval(this.timerId);
|
||||
this.timerId = null;
|
||||
}
|
||||
|
||||
this.resendSeconds.set(0);
|
||||
}
|
||||
|
||||
private isEmail(value: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
||||
}
|
||||
|
||||
private readSession(): AuthSession | null {
|
||||
const raw = sessionStorage.getItem(SESSION_KEY);
|
||||
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const session = JSON.parse(raw) as AuthSession;
|
||||
return this.isEmail(session.email) ? session : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private writeSession(session: AuthSession): void {
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(session));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
@if (prompt.visible()) {
|
||||
<div class="sheet-root">
|
||||
<button
|
||||
class="sheet-root__backdrop"
|
||||
type="button"
|
||||
aria-label="Свернуть"
|
||||
(click)="dismiss()"
|
||||
></button>
|
||||
|
||||
<section class="sheet" role="dialog" aria-modal="true" aria-labelledby="auth-sheet-title">
|
||||
<button
|
||||
class="sheet__handle"
|
||||
type="button"
|
||||
aria-label="Свернуть"
|
||||
(click)="dismiss()"
|
||||
></button>
|
||||
|
||||
@if (auth.step() === 'email') {
|
||||
<div class="sheet__lock" aria-hidden="true">
|
||||
<app-icon name="lock" [size]="28" />
|
||||
</div>
|
||||
|
||||
<h2 id="auth-sheet-title">{{ title() }}</h2>
|
||||
<p class="sheet__lead">
|
||||
Сохраним вашу коллекцию и прогресс. Смотреть города и карту можно без входа.
|
||||
</p>
|
||||
|
||||
<label class="sheet__email">
|
||||
<app-icon name="mail" [size]="20" />
|
||||
<input
|
||||
type="email"
|
||||
inputmode="email"
|
||||
autocomplete="email"
|
||||
placeholder=""
|
||||
[value]="emailDraft()"
|
||||
(input)="onEmail($event)"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<button
|
||||
class="sheet__submit"
|
||||
type="button"
|
||||
[disabled]="!emailValid()"
|
||||
(click)="requestCode()"
|
||||
>
|
||||
Получить код
|
||||
</button>
|
||||
<button class="sheet__ghost" type="button" (click)="dismiss()">Пока просто посмотрю</button>
|
||||
} @else {
|
||||
<h2 id="auth-sheet-title">{{ title() }}</h2>
|
||||
<p class="sheet__lead">
|
||||
Отправили 6 цифр на <b>{{ auth.email() }}</b>
|
||||
</p>
|
||||
|
||||
<div class="sheet__digits">
|
||||
@for (digit of digits(); track $index) {
|
||||
<input
|
||||
#digit
|
||||
type="text"
|
||||
inputmode="numeric"
|
||||
autocomplete="one-time-code"
|
||||
maxlength="1"
|
||||
[attr.aria-label]="'Цифра ' + ($index + 1)"
|
||||
[value]="digit"
|
||||
(input)="onDigit($index, $event)"
|
||||
(keydown)="onDigitKey($index, $event)"
|
||||
(paste)="onPaste($event)"
|
||||
/>
|
||||
}
|
||||
</div>
|
||||
|
||||
<button
|
||||
class="sheet__resend"
|
||||
type="button"
|
||||
[disabled]="auth.resendSeconds() > 0"
|
||||
(click)="resend()"
|
||||
>
|
||||
{{ resendLabel() }}
|
||||
</button>
|
||||
|
||||
<button class="sheet__submit" type="button" [disabled]="!codeReady()" (click)="confirm()">
|
||||
Подтвердить
|
||||
</button>
|
||||
<button class="sheet__ghost" type="button" (click)="changeEmail()">Изменить почту</button>
|
||||
}
|
||||
</section>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
.sheet-root {
|
||||
position: fixed;
|
||||
z-index: 70;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.sheet-root__backdrop {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border: 0;
|
||||
background: rgb(8 7 6 / 64%);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sheet {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
width: min(100%, 480px);
|
||||
max-height: min(92dvh, 760px);
|
||||
overflow: auto;
|
||||
padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
|
||||
border: 1px solid var(--color-border);
|
||||
border-bottom: 0;
|
||||
border-radius: 28px 28px 0 0;
|
||||
background: #1a1815;
|
||||
animation: sheet-in 0.28s ease-out;
|
||||
}
|
||||
|
||||
.sheet__handle {
|
||||
display: block;
|
||||
width: 42px;
|
||||
height: 4px;
|
||||
margin: 6px auto 12px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 4px;
|
||||
background: #4a453c;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sheet__lock {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 68px;
|
||||
height: 68px;
|
||||
margin: 8px auto 16px;
|
||||
border: 1.5px solid var(--color-accent);
|
||||
border-radius: 18px;
|
||||
color: var(--color-accent);
|
||||
}
|
||||
|
||||
.sheet h2 {
|
||||
margin: 0;
|
||||
font-family: var(--font-display);
|
||||
font-size: clamp(26px, 7vw, 32px);
|
||||
font-weight: 700;
|
||||
line-height: 1.15;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sheet__lead {
|
||||
margin: 10px 0 0;
|
||||
color: var(--color-muted);
|
||||
font-size: 15px;
|
||||
line-height: 1.4;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sheet__lead b {
|
||||
color: var(--color-accent-soft);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.sheet__email {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
min-height: 52px;
|
||||
margin-top: 18px;
|
||||
padding: 0 14px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 16px;
|
||||
background: #12110f;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
|
||||
.sheet__email input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--color-text);
|
||||
font: inherit;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.sheet__email input:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.sheet__digits {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.sheet__digits input {
|
||||
width: 46px;
|
||||
height: 56px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 14px;
|
||||
background: #12110f;
|
||||
color: var(--color-text);
|
||||
font: inherit;
|
||||
font-size: 22px;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sheet__digits input:focus {
|
||||
border-color: var(--color-accent);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.sheet__resend {
|
||||
display: block;
|
||||
width: 100%;
|
||||
margin-top: 14px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
background: none;
|
||||
color: var(--color-muted);
|
||||
font: inherit;
|
||||
font-size: 14px;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sheet__resend:disabled {
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.sheet__submit {
|
||||
width: 100%;
|
||||
min-height: 52px;
|
||||
margin-top: 16px;
|
||||
border: 0;
|
||||
border-radius: 16px;
|
||||
background: var(--color-accent);
|
||||
color: var(--color-on-accent);
|
||||
font: inherit;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sheet__submit:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.sheet__ghost {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
min-height: 44px;
|
||||
margin-top: 4px;
|
||||
border: 0;
|
||||
background: none;
|
||||
color: var(--color-muted);
|
||||
font: inherit;
|
||||
font-size: 15px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
@keyframes sheet-in {
|
||||
from {
|
||||
transform: translateY(24px);
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.sheet {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
ElementRef,
|
||||
HostListener,
|
||||
computed,
|
||||
effect,
|
||||
inject,
|
||||
signal,
|
||||
untracked,
|
||||
viewChildren,
|
||||
} from '@angular/core';
|
||||
|
||||
import { AuthPromptService } from '../../auth-prompt.service';
|
||||
import { AuthService } from '../../auth.service';
|
||||
import { Icon } from '../../../shared/icon/icon';
|
||||
|
||||
@Component({
|
||||
selector: 'app-auth-sheet',
|
||||
imports: [Icon],
|
||||
templateUrl: './auth-sheet.html',
|
||||
styleUrl: './auth-sheet.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class AuthSheet {
|
||||
private readonly digitFields = viewChildren<ElementRef<HTMLInputElement>>('digit');
|
||||
|
||||
protected readonly auth = inject(AuthService);
|
||||
protected readonly prompt = inject(AuthPromptService);
|
||||
|
||||
protected readonly emailDraft = signal('');
|
||||
protected readonly digits = signal<string[]>(['', '', '', '', '', '']);
|
||||
|
||||
protected readonly emailValid = computed(() =>
|
||||
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.emailDraft().trim()),
|
||||
);
|
||||
|
||||
protected readonly codeReady = computed(() => this.digits().every((digit) => digit.length === 1));
|
||||
|
||||
protected readonly title = computed(() => {
|
||||
if (this.auth.step() === 'code') {
|
||||
return 'Введите код из письма';
|
||||
}
|
||||
|
||||
return this.prompt.intent() === 'claim'
|
||||
? 'Войдите, чтобы забрать объект'
|
||||
: 'Войдите, чтобы открыть коллекцию';
|
||||
});
|
||||
|
||||
protected readonly resendLabel = computed(() => {
|
||||
const seconds = this.auth.resendSeconds();
|
||||
|
||||
if (seconds <= 0) {
|
||||
return 'Отправить код ещё раз';
|
||||
}
|
||||
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const rest = String(seconds % 60).padStart(2, '0');
|
||||
return `Отправить код ещё раз через ${minutes}:${rest}`;
|
||||
});
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
if (!this.prompt.visible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
untracked(() => {
|
||||
this.emailDraft.set(this.auth.email());
|
||||
this.digits.set(['', '', '', '', '', '']);
|
||||
});
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
if (this.auth.step() !== 'code' || !this.prompt.visible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => this.digitFields()[0]?.nativeElement.focus());
|
||||
});
|
||||
}
|
||||
|
||||
protected onEmail(event: Event): void {
|
||||
const input = event.target as HTMLInputElement;
|
||||
this.emailDraft.set(input.value);
|
||||
}
|
||||
|
||||
protected requestCode(): void {
|
||||
this.digits.set(['', '', '', '', '', '']);
|
||||
this.auth.requestCode(this.emailDraft());
|
||||
}
|
||||
|
||||
protected confirm(): void {
|
||||
if (!this.auth.confirmCode(this.digits().join(''))) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.prompt.finish();
|
||||
}
|
||||
|
||||
protected resend(): void {
|
||||
if (this.auth.resendSeconds() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.digits.set(['', '', '', '', '', '']);
|
||||
this.auth.requestCode(this.auth.email());
|
||||
}
|
||||
|
||||
protected changeEmail(): void {
|
||||
this.auth.changeEmail();
|
||||
}
|
||||
|
||||
protected dismiss(): void {
|
||||
this.prompt.dismiss();
|
||||
}
|
||||
|
||||
protected onDigit(index: number, event: Event): void {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const value = input.value.replace(/\D/g, '').slice(-1);
|
||||
input.value = value;
|
||||
|
||||
this.digits.update((current) => {
|
||||
const next = [...current];
|
||||
next[index] = value;
|
||||
return next;
|
||||
});
|
||||
|
||||
if (value && index < this.digits().length - 1) {
|
||||
this.digitFields()[index + 1]?.nativeElement.focus();
|
||||
}
|
||||
}
|
||||
|
||||
protected onDigitKey(index: number, event: KeyboardEvent): void {
|
||||
if (event.key !== 'Backspace' || this.digits()[index]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const previous = index - 1;
|
||||
|
||||
if (previous < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
this.digits.update((current) => {
|
||||
const next = [...current];
|
||||
next[previous] = '';
|
||||
return next;
|
||||
});
|
||||
this.digitFields()[previous]?.nativeElement.focus();
|
||||
}
|
||||
|
||||
protected onPaste(event: ClipboardEvent): void {
|
||||
const text = event.clipboardData?.getData('text') ?? '';
|
||||
const parsed = text.replace(/\D/g, '').slice(0, 6).split('');
|
||||
|
||||
if (parsed.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
this.digits.set(Array.from({ length: 6 }, (_, index) => parsed[index] ?? ''));
|
||||
|
||||
const focusIndex = Math.min(parsed.length, 5);
|
||||
this.digitFields()[focusIndex]?.nativeElement.focus();
|
||||
}
|
||||
|
||||
@HostListener('document:keydown.escape')
|
||||
protected onEscape(): void {
|
||||
if (this.prompt.visible()) {
|
||||
this.dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user