From b4557c35960d6b0cc15c3aefa1650b34316a99b1 Mon Sep 17 00:00:00 2001 From: Matthew Aaron Raymer Date: Mon, 19 Dec 2022 14:19:33 +0800 Subject: [PATCH] Adding markers to keep track of registration state --- src/router/index.ts | 8 +-- src/store/app.ts | 3 + src/views/AccountViewView.vue | 114 ++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index e1af138..df9ae69 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -105,6 +105,7 @@ const routes: Array = [ }, ]; +/** @type {*} */ const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes, @@ -121,14 +122,9 @@ router.beforeEach(async (to) => { case "uninitialized": return_path = ""; break; - case "registering": - return_path = to.path; - break; - } - } else { - switch (appStore.condition) { case "registered": return_path = to.path; + break; } } if (return_path == "") { diff --git a/src/store/app.ts b/src/store/app.ts index 900904c..3ab7b8c 100644 --- a/src/store/app.ts +++ b/src/store/app.ts @@ -20,5 +20,8 @@ export const useAppStore = defineStore({ reset() { localStorage.removeItem("condition"); }, + setCondition(newCondition: string) { + localStorage.setItem("condition", newCondition); + }, }, }); diff --git a/src/views/AccountViewView.vue b/src/views/AccountViewView.vue index 076352b..1bec4d4 100644 --- a/src/views/AccountViewView.vue +++ b/src/views/AccountViewView.vue @@ -171,6 +171,7 @@ import { createIdentifier, deriveAddress, newIdentifier } from "../libs/crypto"; import { IIdentifier } from "@veramo/core"; import * as R from "ramda"; import { db } from "../db"; +import { useAppStore } from "@/store/app"; @Options({ components: {}, @@ -185,68 +186,77 @@ export default class AccountViewView extends Vue { const previousIdentifiers: Array = []; const toLowercase = true; - this.mnemonic = createIdentifier(); - [ - this.address, - this.privateHex, - this.publicHex, - this.UPORT_ROOT_DERIVATION_PATH, - ] = deriveAddress(this.mnemonic); - //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... derived keys and address..."})) - const prevIds = previousIdentifiers || []; - if (toLowercase) { - const foundEqual = R.find( - (id: IIdentifier) => id.did.split(":")[2] === this.address, - prevIds - ); - if (foundEqual) { - // appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a normal-case version of the DID since a regular version exists."})) + const appCondition = useAppStore().condition; + if (appCondition == "uninitialized") { + this.mnemonic = createIdentifier(); + [ + this.address, + this.privateHex, + this.publicHex, + this.UPORT_ROOT_DERIVATION_PATH, + ] = deriveAddress(this.mnemonic); + //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... derived keys and address..."})) + const prevIds = previousIdentifiers || []; + if (toLowercase) { + const foundEqual = R.find( + (id: IIdentifier) => id.did.split(":")[2] === this.address, + prevIds + ); + if (foundEqual) { + // appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a normal-case version of the DID since a regular version exists."})) + } else { + this.address = this.address.toLowerCase(); + } } else { - this.address = this.address.toLowerCase(); + // They're not trying to convert to lowercase. + const foundLower = R.find( + (id: IIdentifier) => + id.did.split(":")[2] === this.address.toLowerCase(), + prevIds + ); + if (foundLower) { + // appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a lowercase version of the DID since a lowercase version exists."})) + this.address = this.address.toLowerCase(); + } } - } else { - // They're not trying to convert to lowercase. - const foundLower = R.find( - (id: IIdentifier) => - id.did.split(":")[2] === this.address.toLowerCase(), - prevIds + + const newId = newIdentifier( + this.address, + this.publicHex, + this.privateHex, + this.UPORT_ROOT_DERIVATION_PATH ); - if (foundLower) { - // appStore.dispatch(appSlice.actions.addLog({log: true, msg: "Will create a lowercase version of the DID since a lowercase version exists."})) - this.address = this.address.toLowerCase(); - } - } + try { + await db.open(); + const num_accounts = await db.accounts.count(); + if (num_accounts === 0) { + console.log("..."); + await db.accounts.add({ + publicKey: newId.keys[0].publicKeyHex, + mnemonic: this.mnemonic, + identity: JSON.stringify(newId), + dateCreated: new Date().getTime(), + }); + } + useAppStore().setCondition("registered"); - const newId = newIdentifier( - this.address, - this.publicHex, - this.privateHex, - this.UPORT_ROOT_DERIVATION_PATH - ); - try { - await db.open(); - const num_accounts = await db.accounts.count(); - if (num_accounts === 0) { - console.log("..."); - await db.accounts.add({ - publicKey: newId.keys[0].publicKeyHex, - mnemonic: this.mnemonic, - identity: JSON.stringify(newId), - dateCreated: new Date().getTime(), - }); + //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... created new ID..."})) + //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... stored new ID..."})) + } catch (err) { + console.log("Error!"); + console.log(err); } + } + await db.open(); + const num_accounts = await db.accounts.count(); + if (num_accounts === 0) { + console.log("Problem! Should have a profile!"); + } else { const accounts = await db.accounts.toArray(); console.log(accounts[0]); const identity = JSON.parse(accounts[0].identity); - this.address = identity.did; this.publicHex = identity.keys[0].publicKeyHex; - - //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... created new ID..."})) - //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... stored new ID..."})) - } catch (err) { - console.log("Error!"); - console.log(err); } } }