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/icons": "^5.25.0",
"@taiga-ui/kit": "^5.25.0", "@taiga-ui/kit": "^5.25.0",
"@taiga-ui/layout": "^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", "libphonenumber-js": "^1.12.41",
"ngx-cookie-service": "^21.3.1", "ngx-cookie-service": "^21.3.1",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",

View File

@@ -79,7 +79,9 @@
"microsoftStoreDesc": "Start testing the application on your Windows computer via the Microsoft Store!" "microsoftStoreDesc": "Start testing the application on your Windows computer via the Microsoft Store!"
}, },
"signIn": { "signIn": {
"formTitle": "Sign in", "initial": {
"formTitle": "Welcome back!",
"formSubtitle": "Sign in with your Chatenium account.",
"mainInput": { "mainInput": {
"label": "Username, email or phone number", "label": "Username, email or phone number",
"placeholder": "bob, bob@example.com, +36201234567" "placeholder": "bob, bob@example.com, +36201234567"
@@ -87,15 +89,59 @@
"password": { "password": {
"label": "Password" "label": "Password"
}, },
"emailButton": "Sign in with email code", "signInButton": "Sign in"
"smsButton": "Sign in with SMS code", },
"signInButton": "Sign in", "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"
}
}
},
"anyAuth": {
"verifyButton": "Verify identity"
},
"passwordAuth": {
"formTitle": "Password authentication",
"formSubtitle": "Enter your password below",
"errors": { "errors": {
"unameMailPhoneRequired": "Username, email or phone number is required",
"passwordRequired": "Password is required", "passwordRequired": "Password is required",
"passwordIncorrect": "Password is incorrect" "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": { "chat": {
"userSettingsDialog": { "userSettingsDialog": {
"label": "Settings", "label": "Settings",

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"> <main id="holder">
<form [formGroup]="signInForm" tuiAppearance="floating"> @switch (page()) {
@case ('initial') {
<form [formGroup]="signInForm" tuiAppearance="floating" id="initial">
<header> <header>
<h1>{{"signIn.formTitle"|translate}}</h1> <h1>{{ "signIn.initial.formTitle"|translate }}</h1>
<span>{{ "signIn.initial.formSubtitle"|translate }}</span>
</header> </header>
<main> <main>
<tui-textfield iconStart="@tui.user"> <tui-textfield iconStart="@tui.user">
<label tuiLabel>{{"signIn.mainInput.label"|translate}}</label> <label tuiLabel>{{ "signIn.initial.mainInput.label"|translate }}</label>
<input (input)="checkAuthMethods(signInForm.controls['unameMailPhone'].value)" formControlName="unameMailPhone" tuiInput placeholder="{{'signIn.mainInput.placeholder'|translate}}"> <input (input)="checkAuthMethods(signInForm.controls['unameMailPhone'].value)"
formControlName="unameMailPhone" tuiInput placeholder="{{'signIn.mainInput.placeholder'|translate}}">
</tui-textfield> </tui-textfield>
@if (signInForm.controls["unameMailPhone"].dirty) { @if (signInForm.controls["unameMailPhone"].dirty) {
@if (signInForm.controls["unameMailPhone"].hasError("required")) { @if (signInForm.controls["unameMailPhone"].hasError("required")) {
<tui-error [error]="'signIn.errors.unameMailPhoneRequired'|translate"></tui-error> <tui-error [error]="'signIn.errors.unameMailPhoneRequired'|translate"></tui-error>
} }
} }
<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>
}
}
<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>
<footer> <footer>
<button (click)="passwordSignIn(signInForm.controls['unameMailPhone'].value, signInForm.controls['password'].value)" [disabled]="signInForm.invalid || !methods().password" tuiButton> <button (click)="startChallenge(signInForm.controls['unameMailPhone'].value)"
{{'signIn.signInButton'|translate}} [disabled]="signInForm.invalid && false || pendingAction()" tuiButton>
{{ 'signIn.initial.signInButton'|translate }}
</button> </button>
</footer> </footer>
</form> </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>
<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>
<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> </main>

View File

@@ -5,8 +5,8 @@
height: 100vh; height: 100vh;
form { form {
width: 50%; width: 35%;
height: 45%; max-height: 65%;
border-radius: 35px; border-radius: 35px;
background: var(--tui-background-base-alt); background: var(--tui-background-base-alt);
padding: 20px; padding: 20px;
@@ -14,21 +14,51 @@
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
main { header {
align-self: center;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 10px; align-items: center;
margin-bottom: 35px;
margin-top: 25px;
#methods { h1 {
width: 100%; margin: 0;
display: grid; padding: 0;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
} }
} }
footer { 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 {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 {TranslatePipe} from '@ngx-translate/core';
import {TuiButtonGroup, TuiPassword} from '@taiga-ui/kit'; import {TuiButtonGroup, TuiPassword} from '@taiga-ui/kit';
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms'; import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
@@ -10,6 +10,12 @@ import {ServiceManager} from '../service-manager';
import {IndexedDB} from '../storage/indexed-db'; import {IndexedDB} from '../storage/indexed-db';
import {PublicUserData} from '@chatenium/chatenium-sdk/domain/common.schema'; import {PublicUserData} from '@chatenium/chatenium-sdk/domain/common.schema';
import {Router} from '@angular/router'; 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({ @Component({
selector: 'app-signin', selector: 'app-signin',
@@ -24,44 +30,67 @@ import {Router} from '@angular/router';
TuiIcon, TuiIcon,
ReactiveFormsModule, ReactiveFormsModule,
TuiErrorDirective, TuiErrorDirective,
TuiError TuiError,
TuiItemGroup,
TuiGroup,
FaIconComponent,
JsonPipe,
], ],
templateUrl: './signin.html', templateUrl: './signin.html',
styleUrl: './signin.scss', styleUrl: './signin.scss',
}) })
export class SignIn implements OnInit { export class SignIn implements OnInit {
indexedDb = inject(IndexedDB) SDK = inject(CHATENIUM_SDK);
router = inject(Router)
service = new AuthService() challengeApi: AuthChallenge | null = null;
serviceManager = new ServiceManager() challenge = signal<Challenge | null>(null);
methods = signal<AuthMethods>({sms: false, email: false, password: false})
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({ signInForm = new FormGroup({
unameMailPhone: new FormControl('', { 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; timeoutId: any;
async checkAuthMethods(unameMailPhone: string | null) { async checkAuthMethods(unameMailPhone: string | null) {
console.log("Called") console.log('Called');
clearTimeout(this.timeoutId); clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(async () => { this.timeoutId = setTimeout(async () => {
this.methods.set({sms: false, password: false, email: false}) this.methods.set({ sms: false, password: false, email: false });
this.methods.set(await this.service.getAuthMethods(unameMailPhone ?? "")) this.methods.set(await this.service.getAuthMethods(unameMailPhone ?? ''));
}, 500) }, 500);
} }
async passwordSignIn(unameMailPhone: string | null, password: string | null) { async startChallenge(identity: string | null) {
this.pendingAction.set(true);
try { 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 ?? "") const resp = await this.service.loginPasswordAuth(unameMailPhone ?? "", password ?? "")
this.serviceManager.sessionManager.addSession(<PublicUserData>{ this.serviceManager.sessionManager.addSession(<PublicUserData>{
pfp: resp.pfp, pfp: resp.pfp,
@@ -71,13 +100,59 @@ export class SignIn implements OnInit {
}, resp.token) }, resp.token)
console.log("Logged in") console.log("Logged in")
this.router.navigate(["/chat"]) this.router.navigate(["/chat"])
*/
} catch (e) { } catch (e) {
console.error(e) console.error(e);
this.signInForm.controls['password'].setErrors({incorrect: true}) 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() { ngOnInit() {
this.indexedDb.openDatabase() this.indexedDb.openDatabase();
} }
protected readonly faGoogle = faGoogle;
protected readonly faApple = faApple;
} }