Skip to content

Commit

Permalink
Read account upon next load
Browse files Browse the repository at this point in the history
- Add an option to wipe data.
  • Loading branch information
sondreb committed Jul 1, 2024
1 parent d4b5ea0 commit f4697f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
30 changes: 25 additions & 5 deletions app/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, inject, signal } from '@angular/core';
import { StorageService } from './storage.service';
import { CryptoService } from './crypto.service';
import { IdentityService } from './identity.service';
import { Web5ConnectResult } from '@web5/api';

@Injectable({
providedIn: 'root'
Expand All @@ -20,7 +21,17 @@ export class AppService {
async initialize() {
console.log('Initializing Ariton...');

let accounts = this.storage.read('accounts');
let state: any = this.storage.read('state');

if (!state) {
state = {
selectedAccount: ''
};
}

let accounts = this.storage.read('accounts') as any[];

let result: Web5ConnectResult;

console.log('Accounts: ', accounts);

Expand All @@ -35,9 +46,7 @@ export class AppService {

// Initialize the identity service with the password to create an
// initial account.
const result = await this.identity.initialConnect(password);

console.log('Result: ', result);
result = await this.identity.initialConnect(password);

// Save the account to storage.
accounts = [{
Expand All @@ -47,10 +56,21 @@ export class AppService {
}];

this.storage.save('accounts', accounts);
}

state.selectedAccount = result.did;

this.storage.save('state', state);
} else {
// If there are accounts, select the one from the state.selectedAccount value.
const account = accounts.find((account: any) => account.did === state.selectedAccount);

console.log('Previous selected account: ', account);

result = await this.identity.connect(account.did, account.password);
}

console.log('RESULT: ', result);

// let password = this.storage.read('password');

// // If there are no password, either user has choose to not persist
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/introduction/introduction.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ <h1>Welcome</h1>
<button mat-stroked-button [routerLink]="['/communities']">Skip and jump into the app</button>
<button mat-flat-button [routerLink]="['/account/password']">Set a secure password</button>
<button mat-flat-button [routerLink]="['/account/backup']">Backup your account</button>
<button mat-flat-button color="warn" (click)="wipe()">Reset (Wipe) Data</button>

</p>

<mat-divider></mat-divider>
Expand Down
23 changes: 22 additions & 1 deletion app/src/app/introduction/introduction.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatDividerModule } from '@angular/material/divider';
import { RouterLink } from '@angular/router';
import { StorageService } from '../storage.service';

@Component({
selector: 'app-introduction',
Expand All @@ -11,5 +12,25 @@ import { RouterLink } from '@angular/router';
styleUrl: './introduction.component.scss'
})
export class IntroductionComponent {
private storage = inject(StorageService);

async wipe() {
// Clear all data from localStorage
this.storage.clear();

console.log('Local storage data has been wiped!');

// Clear all data from IndexedDb
await indexedDB.deleteDatabase('level-js-DATA/AGENT');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/DID_RESOLVERCACHE');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/DWN_DATASTORE');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/DWN_EVENTLOG');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/DWN_MESSAGEINDEX');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/DWN_MESSAGESTORE');
await indexedDB.deleteDatabase('level-js-DATA/AGENT/VAULT_STORE');

console.log('Data has been wiped!');

window.location.reload();
}
}

0 comments on commit f4697f2

Please sign in to comment.