import { Component } from '@angular/core'; import { AlertController, ToastController, Events } from 'ionic-angular'; import { DateIdUtil } from '@canvuus-internal/mvp0-task-core'; import { LoginOption, UserNickname, UserAccount } from '@canvuus-internal/mvp0-task-auth'; import { TaskStatus, TaskBit, UserProfile } from '@canvuus-internal/mvp0-task-data'; import { CommonEvents } from '../../common/events/common-events'; import { LocalUserService } from '../../services/local-user-service'; @Component({ selector: 'mvp0-user-accounts', template: ` {{localUserProfile.nickname}}

This is your default local account. Warning: Local account data will not be synch'ed with cloud database. If you use a web browser, the data may be lost.

{{mainAccount.nickname}} Keep me logged in

{{mainAccount.primaryAuthIdentity.authId}}

Use it for notifications

{{mainAccount.secondaryAuthIdentity.authId}}

Use it for notifications

{{mainAccount.tertiaryAuthIdentity.authId}}

Use it for notifications Add new identity (email or phone)

Delete This Account:

` }) export class Mvp0UserAccountsComponent { // Temporary // localAccountNickname: string; // public data: UserAccount[]; // tbd: private loginOption: LoginOption; private userNickname: UserNickname; // public activeAccount: UserAccount; private mainAccount: UserAccount; // For now, one account only. // ... localUserProfile: UserProfile; // ... get isShowLocalProfile(): boolean { if (this.localUserProfile && this.localUserProfile.nickname) { return !this.isMainAccountAdded; } else { return false; } } // isMainAccountAdded = false; get isMainAccountAdded(): boolean { if (this.mainAccount && this.mainAccount.isInitialized) { return true; } else { return false; } } get isNewAccountAdditionAllowed(): boolean { // for now, we allow one main account only. return !this.isMainAccountAdded; // if (this.loginOption && this.loginOption.defaultAccountNickname) { // return false; // } else { // return true; // } } // isPrimaryIdentityAdded = false; get isPrimaryIdentityAdded(): boolean { if (this.mainAccount && this.mainAccount.hasPrimaryAuthIdentity) { return true; } else { return false; } } get isPrimaryIdentityValidated(): boolean { if (this.mainAccount && this.mainAccount.hasPrimaryAuthIdentity && this.mainAccount.primaryAuthIdentity && this.mainAccount.primaryAuthIdentity.isTokenValidated) { return true; } else { return false; } } // ???? isPrimaryIdentityAuthenticated = false; // isSecondaryIdentityAdded = false; get isSecondaryIdentityAdded(): boolean { if (this.mainAccount && this.mainAccount.hasSecondaryAuthIdentity) { return true; } else { return false; } } get isSecondaryIdentityValidated(): boolean { if (this.mainAccount && this.mainAccount.hasSecondaryAuthIdentity && this.mainAccount.secondaryAuthIdentity && this.mainAccount.secondaryAuthIdentity.isTokenValidated) { return true; } else { return false; } } // ???? isSecondaryIdentityAuthenticated = false; // isTertiaryIdentityAdded = false; get isTertiaryIdentityAdded(): boolean { if (this.mainAccount && this.mainAccount.hasTertiaryAuthIdentity) { return true; } else { return false; } } get isTertiaryIdentityValidated(): boolean { if (this.mainAccount && this.mainAccount.hasTertiaryAuthIdentity && this.mainAccount.tertiaryAuthIdentity && this.mainAccount.tertiaryAuthIdentity.isTokenValidated) { return true; } else { return false; } } // ???? isTertiaryIdentityAuthenticated = false; // Note that we currently support max of 3 identities per account. get isAllIdentitiesAdded(): boolean { return this.isPrimaryIdentityAdded && this.isSecondaryIdentityAdded && this.isTertiaryIdentityAdded; } get hasAtLeastOneIdentity(): boolean { return this.isPrimaryIdentityAdded || this.isSecondaryIdentityAdded || this.isTertiaryIdentityAdded; } get isAccountAuthenticated(): boolean { return this.isPrimaryIdentityAuthenticated || this.isSecondaryIdentityAuthenticated || this.isTertiaryIdentityAuthenticated; } // ... // tbd: get canDeletePrimaryIdentity(): boolean { // return (this.isPrimaryIdentityAdded && !this.isSecondaryIdentityAdded); return false; } get canDeleteSecondaryIdentity(): boolean { return (this.isSecondaryIdentityAdded && !this.isTertiaryIdentityAdded); } get canDeleteTertiaryIdentity(): boolean { return (this.isTertiaryIdentityAdded); } constructor(private alertCtrl: AlertController, private toastCtrl: ToastController, private events: Events, private localUserService: LocalUserService ) { // console.log('Mvp0UserAccountsComponent: Hello Mvp0UserAccounts Component'); // TBD: // this.localAccountNickname = 'Joe'; // Note that all UI-accessed variables should be initialized here in some way // to avoid null pointer exception.... // temporary // this.data = []; // this.activeAccount = null; let userId = this.localUserService.currentUserId; this.loginOption = new LoginOption(); this.userNickname = new UserNickname(userId); this.mainAccount = new UserAccount(userId); this.localUserProfile = new UserProfile(userId); // ... } // // tbd // // refreshData(_data: UserAccount[]): void { // // console.log('Mvp0UserAccountsComponent: >>> refreshData() _data = ' + (_data) ? _data.toString() : ""); // // if (_data) { // // let list: UserAccount[] = []; // // for(let ua of _data) { // // list.push(ua.clone()); // // } // // this.data = list; // // } // // console.log('Mvp0UserAccountsComponent: >>> refreshData() this.data = ' + this.data.toString()); // // } // refreshData(_mainAccount: UserAccount): void { // console.log('Mvp0UserAccountsComponent: >>>>> refreshData() _mainAccount = ' + _mainAccount); // if (_mainAccount) { // this.mainAccount = _mainAccount.clone(); // } else { // // ??? // } // console.log('Mvp0UserAccountsComponent: >>> refreshData() this.mainAccount = ' + this.mainAccount); // } refreshLoginOption(_loginOption: LoginOption) { console.log('Mvp0UserAccountsComponent: >>>>> refreshLoginOption() _loginOption = ' + _loginOption); if (_loginOption) { this.loginOption = _loginOption.clone(); } else { // ??? } } refreshUserNickname(_userNickname: UserNickname) { console.log('Mvp0UserAccountsComponent: >>>>> refreshUserNickname() _userNickname = ' + _userNickname); if (_userNickname) { this.userNickname = _userNickname.clone(); } else { // ??? } } refreshMainAccount(_userAccount: UserAccount) { console.log('Mvp0UserAccountsComponent: >>>>> refreshMainAccount() _userAccount = ' + _userAccount); if (_userAccount) { this.mainAccount = _userAccount.clone(); // temporary // this.isMainAccountAdded = true; } else { // ??? } } refreshLocalUserProfile(_userProfile: UserProfile) { console.log('Mvp0UserAccountsComponent: >>>>> refreshLocalUserProfile() _userProfile = ' + _userProfile); if (_userProfile) { this.localUserProfile = _userProfile.clone(); } else { // ??? } } handleActionAddNewAccount() { // console.log('Mvp0UserAccountsComponent: handleActionAddNewAccount()'); this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_CREATE); } handleActionDeleteAccount() { // console.log('Mvp0UserAccountsComponent: handleActionDeleteAccount()'); let alert = this.alertCtrl.create({ title: 'Remove Account', message: 'Do you want to delete this account from your account list?', buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'Remove', handler: () => { console.log('Mvp0UserAccountsComponent: Remove clicked'); // tbd: this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_DELETE, this.mainAccount); } } ] }); alert.present(); } handleActionAccountLogin() { // console.log('Mvp0UserAccountsComponent: handleActionAccountLogin()'); this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_LOGIN, this.mainAccount); } handleActionAccountLogout() { // console.log('Mvp0UserAccountsComponent: handleActionAccountLogout()'); this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_LOGOUT, this.mainAccount); } handleActionChangeNickname() { // console.log('Mvp0UserAccountsComponent: handleActionChangeNickname()'); this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_CHANGENAME, this.mainAccount); } handleActionChangePassword() { // console.log('Mvp0UserAccountsComponent: handleActionChangePassword()'); let alert = this.alertCtrl.create({ title: 'Change Password', message: 'Please specify old and new passwords.', inputs: [ { name: 'oldPassword', placeholder: 'Old Password' }, { name: 'newPassword', placeholder: 'New Password' } ], buttons: [ { text: 'Cancel', role: 'cancel', handler: data => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'Confirm', handler: data => { let that = this; if (data.oldPassword && data.newPassword) { // TBD: let oldpword = data.oldPassword as string; let newpword = data.newPassword as string; // console.log('Mvp0UserAccountsComponent: Confirm clicked.'); that.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_CHANGEPASS, oldpword, newpword); // return true; } else { // tbd: // just cancel? console.log('Mvp0UserAccountsComponent: Passwords not specified.'); // return false; } return true; } } ] }); alert.present(); } handleActionKeepLoggedOn(event) { console.log('Mvp0UserAccountsComponent: handleActionKeepLoggedOn(). event = ' + event); let isOn = event; this.events.publish(CommonEvents.MVP0_AUTH_ACCOUNT_KEEPLOGGEDON, isOn); } handleActionVerifyIdentity(identityOrdinal: number) { // console.log('Mvp0UserAccountsComponent: handleActionVerifyIdentity(). identityOrdinal + ' + identityOrdinal); // tbd let authId = ""; let alert = this.alertCtrl.create({ title: 'Verify Identity', message: 'Please input validation token for ' + authId, inputs: [ { name: 'validationToken', placeholder: 'Token' } ], buttons: [ { text: 'Resend', handler: data => { console.log('Mvp0UserAccountsComponent: Resend clicked'); // ... } }, { text: 'Cancel', role: 'cancel', handler: data => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'Validate', handler: data => { let that = this; if (data.validationToken) { // TBD: let token = data.validationToken as string; console.log('Mvp0UserAccountsComponent: Validate clicked: token = ' + token); that.events.publish(CommonEvents.MVP0_AUTH_IDENTITY_VERIFY, identityOrdinal, token); // return true; } else { // tbd: // just cancel? console.log('Mvp0UserAccountsComponent: token not specified.'); // return false; } return true; } } ] }); alert.present(); } handleActionResetPassword(identityOrdinal: number) { // console.log('Mvp0UserAccountsComponent: handleActionResetPassword(). identityOrdinal + ' + identityOrdinal); // TBD: // Start token request process here ???? // ..... // tbd let authId = ""; let alert = this.alertCtrl.create({ title: 'Reset Password', message: 'Please input validation token for ' + authId + ', and a new password.', inputs: [ { name: 'validationToken', placeholder: 'Token' }, { name: 'newPassword', placeholder: 'New Password' } ], buttons: [ { text: 'Reset', handler: data => { console.log('Mvp0UserAccountsComponent: Reset clicked'); // ... } }, { text: 'Cancel', role: 'cancel', handler: data => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'Confirm', handler: data => { let that = this; if (data.validationToken) { // TBD: let token = data.validationToken as string; let newpword = data.newPassword as string; console.log('Mvp0UserAccountsComponent: Confirm clicked: token = ' + token); that.events.publish(CommonEvents.MVP0_AUTH_IDENTITY_RESETPASS, identityOrdinal, token, newpword); // return true; } else { // tbd: // just cancel? console.log('Mvp0UserAccountsComponent: token not specified.'); // return false; } return true; } } ] }); alert.present(); } // TBD: Use toggle button??? handleActionUseForNotice(identityOrdinal: number, event) { console.log('Mvp0UserAccountsComponent: handleActionUseForNotice(). identityOrdinal + ' + identityOrdinal + '; event = ' + event); // tbd: let isOn = event as boolean; this.events.publish(CommonEvents.MVP0_AUTH_IDENTITY_SETACTIVE, identityOrdinal, isOn); } handleActionAddNewIdentity() { // console.log('Mvp0UserAccountsComponent: handleActionAddNewIdentity()'); if (this.isTertiaryIdentityAdded) { // ignore // No more identities can be added. } else { // 1-based, not 0-based. let identityOrdinal: number = -1; // Invalid if (this.isSecondaryIdentityAdded) { identityOrdinal = 3; } else { if (this.isPrimaryIdentityAdded) { identityOrdinal = 2; } else { // this cannot happen.. identityOrdinal = 1; } } let alert = this.alertCtrl.create({ title: 'New Identity', message: 'Please specify a new email or phone number.', inputs: [ { name: 'userEmail', placeholder: 'Email or Phone' } ], buttons: [ { text: 'Cancel', role: 'cancel', handler: data => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'OK', handler: data => { let that = this; if (data.userEmail) { // TBD: // validate ??? // email should contain at least '@'... // phone number should be all numbers and '-'.... let emailOrPhone = data.userEmail as string; console.log('Mvp0UserAccountsComponent: OK clicked: emailOrPhone = ' + emailOrPhone); that.events.publish(CommonEvents.MVP0_AUTH_IDENTITY_CREATE, identityOrdinal, emailOrPhone); // return true; } else { // tbd: // just cancel? console.log('Mvp0UserAccountsComponent: userEmail not specified.'); // return false; } return true; } } ] }); alert.present(); } } // Currently not being used. // For now, you cannot deleted an identity once added. handleActionDeleteIdentity(identityOrdinal: number) { // console.log('Mvp0UserAccountsComponent: handleActionDeleteIdentity() identityOrdinal = ' + identityOrdinal); // tbd: // check first if the identity of the given identityOrdinal can be deleted?? // .... let alert = this.alertCtrl.create({ title: 'Remove Identity', message: 'Do you want to delete this identity from your account?', buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { console.log('Mvp0UserAccountsComponent: Cancel clicked'); } }, { text: 'Remove', handler: () => { console.log('Mvp0UserAccountsComponent: Remove clicked'); // tbd: this.events.publish(CommonEvents.MVP0_AUTH_IDENTITY_DELETE, identityOrdinal); } } ] }); alert.present(); } }