Started rewriting SignIn component to use new V3 API. TODO methods: Google, Apple and SMS
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user