Started rewriting SignIn component to use new V3 API. TODO methods: Google, Apple and SMS

This commit is contained in:
Noel Maróti
2026-11-22 19:36:44 +01:00
parent aa3e1de412
commit eeee9d5154
7 changed files with 1706 additions and 164 deletions

1433
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -36,6 +36,7 @@
"@taiga-ui/icons": "^5.25.0",
"@taiga-ui/kit": "^5.25.0",
"@taiga-ui/layout": "^5.25.0",
"chatenium-sdk-preview": "npm:@chatenium/chatenium-sdk@^2.0.0-preview.4",
"libphonenumber-js": "^1.12.41",
"ngx-cookie-service": "^21.3.1",
"rxjs": "~7.8.0",

View File

@@ -79,21 +79,67 @@
"microsoftStoreDesc": "Start testing the application on your Windows computer via the Microsoft Store!"
},
"signIn": {
"formTitle": "Sign in",
"mainInput": {
"label": "Username, email or phone number",
"placeholder": "bob, bob@example.com, +36201234567"
"initial": {
"formTitle": "Welcome back!",
"formSubtitle": "Sign in with your Chatenium account.",
"mainInput": {
"label": "Username, email or phone number",
"placeholder": "bob, bob@example.com, +36201234567"
},
"password": {
"label": "Password"
},
"signInButton": "Sign in"
},
"password": {
"label": "Password"
"methodChoose": {
"formTitle": "Choose a verification method",
"formSubtitle": "Based on your security settings, more than 1 verification method may be required.",
"formTitleAlt": "Choose another verification method",
"formSubtitleAlt": "Because of your security settings, multiple verification methods are required",
"methods": {
"password": {
"title": "Password",
"desc": "Use your password to verify your identity"
},
"mail": {
"title": "E-mail",
"desc": "Use a one-time e-mail code to verify your identity"
},
"sms": {
"title": "SMS",
"desc": "Use a one-time SMS code to verify your identity"
},
"google": {
"title": "Google",
"desc": "Use your linked Google account to verify your identity"
},
"apple": {
"title": "Apple",
"desc": "Use your linked Apple account to verify your identity"
}
}
},
"emailButton": "Sign in with email code",
"smsButton": "Sign in with SMS code",
"signInButton": "Sign in",
"errors": {
"unameMailPhoneRequired": "Username, email or phone number is required",
"passwordRequired": "Password is required",
"passwordIncorrect": "Password is incorrect"
"anyAuth": {
"verifyButton": "Verify identity"
},
"passwordAuth": {
"formTitle": "Password authentication",
"formSubtitle": "Enter your password below",
"errors": {
"passwordRequired": "Password is required",
"passwordIncorrect": "Password is incorrect"
}
},
"emailAuth": {
"formTitle": "E-mail authentication",
"formSubtitle": "Enter your one-time e-mail code below",
"errors": {
"passwordRequired": "Code is required",
"passwordIncorrect": "Code is incorrect"
},
"emailCode": {
"label": "E-mail code"
}
}
},
"chat": {

View File

@@ -0,0 +1,12 @@
import { InjectionToken } from '@angular/core';
import { environment } from '../environments/environment';
import { ChateniumClient } from 'chatenium-sdk-preview';
export const CHATENIUM_SDK = new InjectionToken<ChateniumClient>('CHATENIUM_SDK', {
providedIn: 'root',
factory: () => {
return new ChateniumClient({
baseUrl: environment.api_url
});
},
});

View File

@@ -1,44 +1,155 @@
<main id="holder">
<form [formGroup]="signInForm" tuiAppearance="floating">
<header>
<h1>{{"signIn.formTitle"|translate}}</h1>
</header>
@switch (page()) {
@case ('initial') {
<form [formGroup]="signInForm" tuiAppearance="floating" id="initial">
<header>
<h1>{{ "signIn.initial.formTitle"|translate }}</h1>
<span>{{ "signIn.initial.formSubtitle"|translate }}</span>
</header>
<main>
<tui-textfield iconStart="@tui.user">
<label tuiLabel>{{"signIn.mainInput.label"|translate}}</label>
<main>
<tui-textfield iconStart="@tui.user">
<label tuiLabel>{{ "signIn.initial.mainInput.label"|translate }}</label>
<input (input)="checkAuthMethods(signInForm.controls['unameMailPhone'].value)" formControlName="unameMailPhone" tuiInput placeholder="{{'signIn.mainInput.placeholder'|translate}}">
</tui-textfield>
@if (signInForm.controls["unameMailPhone"].dirty) {
@if (signInForm.controls["unameMailPhone"].hasError("required")) {
<tui-error [error]="'signIn.errors.unameMailPhoneRequired'|translate"></tui-error>
}
}
<input (input)="checkAuthMethods(signInForm.controls['unameMailPhone'].value)"
formControlName="unameMailPhone" tuiInput placeholder="{{'signIn.mainInput.placeholder'|translate}}">
</tui-textfield>
@if (signInForm.controls["unameMailPhone"].dirty) {
@if (signInForm.controls["unameMailPhone"].hasError("required")) {
<tui-error [error]="'signIn.errors.unameMailPhoneRequired'|translate"></tui-error>
}
}
</main>
<tui-textfield iconStart="@tui.key">
<label tuiLabel>{{"signIn.password.label"|translate}}</label>
<input formControlName="password" [disabled]="!methods().password" type="password" tuiInput>
<tui-icon tuiPassword></tui-icon>
</tui-textfield>
@if (signInForm.controls["password"].dirty) {
@if (signInForm.controls["password"].hasError("required")) {
<tui-error [error]="'signIn.errors.passwordRequired'|translate"></tui-error>
} @else if (signInForm.controls["password"].hasError("incorrect")) {
<tui-error [error]="'signIn.errors.passwordIncorrect'|translate"></tui-error>
}
}
<footer>
<button (click)="startChallenge(signInForm.controls['unameMailPhone'].value)"
[disabled]="signInForm.invalid && false || pendingAction()" tuiButton>
{{ 'signIn.initial.signInButton'|translate }}
</button>
</footer>
</form>
}
@case ('methodChoose') {
<form id="methodChoose">
<header>
@if (isMultistep()) {
<h1>{{ "signIn.methodChoose.formTitleAlt"|translate }}</h1>
<span>{{ "signIn.methodChoose.formSubtitleAlt"|translate }}</span>
} @else {
<h1>{{ "signIn.methodChoose.formTitle"|translate }}</h1>
<span>{{ "signIn.methodChoose.formSubtitle"|translate }}</span>
}
</header>
<div id="methods">
<button [disabled]="!methods().email" iconStart="@tui.mail-open" appearance="secondary" tuiButton disabled>{{'signIn.emailButton'|translate}}</button>
<button [disabled]="!methods().sms" iconStart="@tui.phone" appearance="secondary" tuiButton disabled>{{'signIn.smsButton'|translate}}</button>
</div>
</main>
<main tuiGroup orientation="vertical" style="gap: 0">
@if (challenge != null) {
@if (challenge()!.factorsTracker["PASSWORD"] != null) {
<button [disabled]="challenge()!.factorsTracker['PASSWORD'].status == 'SUCCESS'" type="button" tuiButton
appearance="secondary" class="method" (click)="this.page.set('passwordAuth')">
<tui-icon icon="@tui.key"></tui-icon>
<div>
<span class="title">{{ "signIn.methodChoose.methods.password.title"|translate }}</span>
<span class="desc">{{ "signIn.methodChoose.methods.password.desc"|translate }}</span>
</div>
</button>
}
@if (challenge()!.factorsTracker["EMAIL"] != null) {
<button type="button" [disabled]="challenge()!.factorsTracker['EMAIL'].status == 'SUCCESS' || pendingAction()" tuiButton
appearance="secondary" class="method" (click)="this.requestCredential('EMAIL')">
<tui-icon icon="@tui.mail"></tui-icon>
<div>
<span class="title">{{ "signIn.methodChoose.methods.mail.title"|translate }}</span>
<span class="desc">{{ "signIn.methodChoose.methods.mail.desc"|translate }}</span>
</div>
</button>
}
<button tuiButton appearance="secondary" class="method">
<tui-icon icon="@tui.phone"></tui-icon>
<div>
<span class="title">{{ "signIn.methodChoose.methods.sms.title"|translate }}</span>
<span class="desc">{{ "signIn.methodChoose.methods.sms.desc"|translate }}</span>
</div>
</button>
<button tuiButton appearance="secondary" class="method">
<fa-icon [icon]="faGoogle" style="font-size: 25px; margin-left: -5px"></fa-icon>
<footer>
<button (click)="passwordSignIn(signInForm.controls['unameMailPhone'].value, signInForm.controls['password'].value)" [disabled]="signInForm.invalid || !methods().password" tuiButton>
{{'signIn.signInButton'|translate}}
</button>
</footer>
</form>
<div>
<span class="title">{{ "signIn.methodChoose.methods.google.title"|translate }}</span>
<span class="desc">{{ "signIn.methodChoose.methods.google.desc"|translate }}</span>
</div>
</button>
<button tuiButton appearance="secondary" class="method">
<fa-icon [icon]="faApple" style="font-size: 30px; margin-left: -7px"></fa-icon>
<div>
<span class="title">{{ "signIn.methodChoose.methods.apple.title"|translate }}</span>
<span class="desc">{{ "signIn.methodChoose.methods.apple.desc"|translate }}</span>
</div>
</button>
}
</main>
</form>
}
@case ('passwordAuth') {
<form [formGroup]="authForm" tuiAppearance="floating" id="passwordAuth">
<header>
<h1>{{ "signIn.passwordAuth.formTitle"|translate }}</h1>
<span>{{ "signIn.passwordAuth.formSubtitle"|translate }}</span>
</header>
<main>
<tui-textfield iconStart="@tui.key">
<label tuiLabel>{{ "signIn.initial.password.label"|translate }}</label>
<input formControlName="method" type="password" tuiInput>
<tui-icon tuiPassword></tui-icon>
</tui-textfield>
@if (authForm.controls["method"].dirty) {
@if (authForm.controls["method"].hasError("required")) {
<tui-error [error]="'signIn.passwordAuth.errors.passwordRequired'|translate"></tui-error>
} @else if (authForm.controls["method"].hasError("incorrect")) {
<tui-error [error]="'signIn.passwordAuth.errors.passwordIncorrect'|translate"></tui-error>
}
}
</main>
<footer>
<button (click)="verifyMethod('PASSWORD', authForm.controls['method'].value)"
[disabled]="authForm.invalid && false || pendingAction()" tuiButton>
{{ 'signIn.anyAuth.verifyButton'|translate }}
</button>
</footer>
</form>
}
@case ('emailAuth') {
<form [formGroup]="authForm" tuiAppearance="floating" id="emailAuth">
<header>
<h1>{{ "signIn.emailAuth.formTitle"|translate }}</h1>
<span>{{ "signIn.emailAuth.formSubtitle"|translate }}</span>
</header>
<main>
<tui-textfield iconStart="@tui.mail">
<label tuiLabel>{{ "signIn.emailAuth.emailCode.label"|translate }}</label>
<input formControlName="method" type="number" tuiInput>
</tui-textfield>
@if (authForm.controls["method"].dirty) {
@if (authForm.controls["method"].hasError("required")) {
<tui-error [error]="'signIn.emailAuth.errors.passwordRequired'|translate"></tui-error>
} @else if (authForm.controls["method"].hasError("incorrect")) {
<tui-error [error]="'signIn.emailAuth.errors.passwordIncorrect'|translate"></tui-error>
}
}
</main>
<footer>
<button (click)="verifyMethod('EMAIL', authForm.controls['method'].value)"
[disabled]="authForm.invalid && false || pendingAction()" tuiButton>
{{ 'signIn.anyAuth.verifyButton'|translate }}
</button>
</footer>
</form>
}
@case ('ok') {
{{challenge()|json}}
}
}
</main>

View File

@@ -5,8 +5,8 @@
height: 100vh;
form {
width: 50%;
height: 45%;
width: 35%;
max-height: 65%;
border-radius: 35px;
background: var(--tui-background-base-alt);
padding: 20px;
@@ -14,21 +14,51 @@
flex-direction: column;
justify-content: center;
main {
header {
align-self: center;
display: flex;
flex-direction: column;
gap: 10px;
align-items: center;
margin-bottom: 35px;
margin-top: 25px;
#methods {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
h1 {
margin: 0;
padding: 0;
}
}
footer {
margin-top: 30px;
margin-top: 10px;
button {
width: 100%;
}
}
}
#initial main {
display: flex;
flex-direction: column;
gap: 10px;
}
#methodChoose main .method {
width: 100%;
height: 65px;
display: flex;
gap: 15px;
justify-content: start;
.title {
display: flex;
flex-direction: column;
gap: 5px;
align-items: start;
}
.desc {
font-weight: normal;
}
}
}

View File

@@ -1,5 +1,5 @@
import {Component, inject, OnInit, signal} from '@angular/core';
import {TuiAppearance, TuiButton, TuiError, TuiErrorDirective, TuiIcon, TuiInput, TuiTextfield} from '@taiga-ui/core';
import {TuiAppearance, TuiButton, TuiError, TuiErrorDirective, TuiGroup, TuiIcon, TuiInput, TuiTextfield} from '@taiga-ui/core';
import {TranslatePipe} from '@ngx-translate/core';
import {TuiButtonGroup, TuiPassword} from '@taiga-ui/kit';
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
@@ -10,6 +10,12 @@ import {ServiceManager} from '../service-manager';
import {IndexedDB} from '../storage/indexed-db';
import {PublicUserData} from '@chatenium/chatenium-sdk/domain/common.schema';
import {Router} from '@angular/router';
import { CHATENIUM_SDK } from '../chatenium.token';
import { AuthChallenge, Challenge } from 'chatenium-sdk-preview';
import { TuiItemGroup } from '@taiga-ui/layout';
import { faApple, faGoogle } from '@fortawesome/free-brands-svg-icons';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { JsonPipe } from '@angular/common';
@Component({
selector: 'app-signin',
@@ -24,44 +30,67 @@ import {Router} from '@angular/router';
TuiIcon,
ReactiveFormsModule,
TuiErrorDirective,
TuiError
TuiError,
TuiItemGroup,
TuiGroup,
FaIconComponent,
JsonPipe,
],
templateUrl: './signin.html',
styleUrl: './signin.scss',
})
export class SignIn implements OnInit {
indexedDb = inject(IndexedDB)
router = inject(Router)
SDK = inject(CHATENIUM_SDK);
service = new AuthService()
serviceManager = new ServiceManager()
methods = signal<AuthMethods>({sms: false, email: false, password: false})
challengeApi: AuthChallenge | null = null;
challenge = signal<Challenge | null>(null);
private clickEvent = signal(0)
indexedDb = inject(IndexedDB);
router = inject(Router);
service = new AuthService();
serviceManager = new ServiceManager();
methods = signal<AuthMethods>({ sms: false, email: false, password: false });
pendingAction = signal(false);
page = signal<'initial' | 'methodChoose' | 'passwordAuth' | 'emailAuth' | 'smsAuth' | 'ok'>('initial');
isMultistep = signal(false);
signInForm = new FormGroup({
unameMailPhone: new FormControl('', {
validators: [Validators.required]
validators: [Validators.required],
}),
password: new FormControl('', {
validators: [Validators.required]
})
})
});
authForm = new FormGroup({
method: new FormControl('', {
validators: [Validators.required],
}),
});
timeoutId: any;
async checkAuthMethods(unameMailPhone: string | null) {
console.log("Called")
console.log('Called');
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(async () => {
this.methods.set({sms: false, password: false, email: false})
this.methods.set(await this.service.getAuthMethods(unameMailPhone ?? ""))
}, 500)
this.methods.set({ sms: false, password: false, email: false });
this.methods.set(await this.service.getAuthMethods(unameMailPhone ?? ''));
}, 500);
}
async passwordSignIn(unameMailPhone: string | null, password: string | null) {
async startChallenge(identity: string | null) {
this.pendingAction.set(true);
try {
const fetchResp = await this.SDK.auth.getIdFromIdentity(identity ?? '');
const challenge = new AuthChallenge(this.SDK.auth);
const resp = await challenge.start('AUTH:SIGNIN', fetchResp.data.userId);
this.challenge.set(resp);
this.challengeApi = challenge;
this.pendingAction.set(false);
this.page.set('methodChoose');
/*
const resp = await this.service.loginPasswordAuth(unameMailPhone ?? "", password ?? "")
this.serviceManager.sessionManager.addSession(<PublicUserData>{
pfp: resp.pfp,
@@ -71,13 +100,59 @@ export class SignIn implements OnInit {
}, resp.token)
console.log("Logged in")
this.router.navigate(["/chat"])
*/
} catch (e) {
console.error(e)
this.signInForm.controls['password'].setErrors({incorrect: true})
console.error(e);
this.signInForm.controls['unameMailPhone'].setErrors({ incorrect: true });
this.pendingAction.set(false);
}
}
async verifyMethod(methodName: string, credential: string | null) {
this.pendingAction.set(true);
if (this.challengeApi != null) {
try {
const resp = await this.challengeApi.verifyFactor(methodName, String(credential) ?? '');
this.challenge.set(resp);
this.pendingAction.set(false);
if (resp.authIdentityToken == null) {
this.isMultistep.set(true);
this.page.set('methodChoose');
this.authForm.reset();
} else {
this.page.set("ok")
}
} catch (e) {
this.pendingAction.set(false);
this.authForm.controls['method'].setErrors({ incorrect: true });
console.error(e);
}
}
}
async requestCredential(methodName: string) {
this.pendingAction.set(true);
if (this.challengeApi != null) {
try {
const resp = await this.challengeApi.requestCredential(methodName);
this.challenge.set(resp);
this.pendingAction.set(false);
if (resp.authIdentityToken == null) {
this.isMultistep.set(true);
this.page.set(methodName == "EMAIL" ? 'emailAuth' : 'smsAuth');
}
} catch (e) {
this.pendingAction.set(false);
this.authForm.controls['method'].setErrors({ incorrect: true });
console.error(e);
}
}
}
ngOnInit() {
this.indexedDb.openDatabase()
this.indexedDb.openDatabase();
}
protected readonly faGoogle = faGoogle;
protected readonly faApple = faApple;
}