diff --git a/app.vue b/app.vue index 722cc597c..4a53d95fa 100644 --- a/app.vue +++ b/app.vue @@ -43,7 +43,8 @@ import { useUIStore } from '~/stores/ui' const { SITE_URL } = useRuntimeConfig().public const bookStoreApiStore = useBookStoreApiStore() -const { restoreSession } = bookStoreApiStore + +const { restoreAuthSession } = bookStoreApiStore const { isRestoringSession } = storeToRefs(bookStoreApiStore) const uiStore = useUIStore() @@ -91,7 +92,7 @@ useSeoMeta({ }) onMounted(async () => { - await restoreSession() + await restoreAuthSession() }) diff --git a/composables/useAuth.ts b/composables/useAuth.ts index f96c1f83c..b0cc89228 100644 --- a/composables/useAuth.ts +++ b/composables/useAuth.ts @@ -7,7 +7,7 @@ export function useAuth () { const bookStoreApiStore = useBookStoreApiStore() const store = useWalletStore() const { wallet, signer } = storeToRefs(store) - const { connect, disconnect, signMessageMemo } = store + const { disconnect, signMessageMemo, openConnectWalletModal, initWallet } = store const { authenticate, clearSession } = bookStoreApiStore const toast = useToast() @@ -19,10 +19,11 @@ export function useAuth () { setupPostAuthRedirect() if (!wallet.value || !signer.value) { - await connect() - } - if (!wallet.value || !signer.value) { - return + const connection = await openConnectWalletModal() + if (!connection) { + throw new Error('WALLET_NOT_INITED') + } + await initWallet(connection) } const signature = await signMessageMemo( diff --git a/pages/mint-nft/index.vue b/pages/mint-nft/index.vue index 82a71fb8c..5ea0b4a5f 100644 --- a/pages/mint-nft/index.vue +++ b/pages/mint-nft/index.vue @@ -442,7 +442,7 @@ const route = useRoute() const store = useWalletStore() const { wallet, signer } = storeToRefs(store) -const { connect } = store +const { initIfNecessary } = store const appLikeCoURL = APP_LIKE_CO_URL const likerLandURL = LIKER_LAND_URL @@ -570,7 +570,7 @@ async function onISCNFileInput () { try { isLoading.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('NO_WALLET') } if (!iscnCreateData.value) { throw new Error('NO_ISCN_DATA') } @@ -715,7 +715,7 @@ async function onClassFileInput () { try { isLoading.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { return } if (!classCreateData.value) { throw new Error('NO_CLASS_DATA') } @@ -756,7 +756,7 @@ async function onMintNFTStart () { try { isLoading.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { return } if (!nftMintDefaultData.value) { throw new Error('NO_MINT_DATA') } diff --git a/pages/nft-book-store/collection/new.vue b/pages/nft-book-store/collection/new.vue index 1558689dd..5860352bd 100644 --- a/pages/nft-book-store/collection/new.vue +++ b/pages/nft-book-store/collection/new.vue @@ -447,7 +447,7 @@ const collectionStore = useCollectionStore() const nftStore = useNftStore() const stripeStore = useStripeStore() const { wallet, signer } = storeToRefs(walletStore) -const { connect } = walletStore +const { initIfNecessary } = walletStore const { newNFTBookCollection } = collectionStore const { getClassMetadataById, lazyFetchClassMetadataById } = nftStore const { fetchStripeConnectStatusByWallet } = stripeStore @@ -701,7 +701,7 @@ async function submitNewCollection () { if (formattedPrice.stock > 0) { if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('Unable to connect to wallet') diff --git a/pages/nft-book-store/collection/send/[collectionId].vue b/pages/nft-book-store/collection/send/[collectionId].vue index 7e4aeca84..30c79fa97 100644 --- a/pages/nft-book-store/collection/send/[collectionId].vue +++ b/pages/nft-book-store/collection/send/[collectionId].vue @@ -175,7 +175,7 @@ import { AUTHOR_MESSAGE_LIMIT } from '~/constant' const { LIKE_CO_API, LCD_URL } = useRuntimeConfig().public const store = useWalletStore() const { wallet, signer } = storeToRefs(store) -const { connect } = store +const { initIfNecessary } = store const bookStoreApiStore = useBookStoreApiStore() const { token } = storeToRefs(bookStoreApiStore) @@ -290,7 +290,7 @@ async function fetchNextNFTId (count = 1) { nftIdError.value = '' isAutoFetchingNFTId.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!ownerWallet.value) { return } await Promise.all(classIds.value.map(async (classId, index) => { @@ -321,7 +321,7 @@ async function onSendNFTStart () { try { isLoading.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { return } diff --git a/pages/nft-book-store/collection/status/[collectionId]/edit.vue b/pages/nft-book-store/collection/status/[collectionId]/edit.vue index 0f507a0c0..5e5d098f3 100644 --- a/pages/nft-book-store/collection/status/[collectionId]/edit.vue +++ b/pages/nft-book-store/collection/status/[collectionId]/edit.vue @@ -248,7 +248,7 @@ import { deliverMethodOptions } from '~/utils' const collectionStore = useCollectionStore() const nftStore = useNftStore() const walletStore = useWalletStore() -const { connect } = walletStore +const { initIfNecessary } = walletStore const { wallet, signer } = storeToRefs(walletStore) const router = useRouter() @@ -463,7 +463,7 @@ async function handleSubmit () { let autoDeliverNFTsTxHash if (newAutoDeliverNFTsCount > 0) { if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('Unable to connect to wallet') diff --git a/pages/nft-book-store/new.vue b/pages/nft-book-store/new.vue index 670ddfea9..3293b2922 100644 --- a/pages/nft-book-store/new.vue +++ b/pages/nft-book-store/new.vue @@ -502,7 +502,7 @@ const { LCD_URL } = useRuntimeConfig().public const walletStore = useWalletStore() const bookStoreApiStore = useBookStoreApiStore() const stripeStore = useStripeStore() -const { connect } = walletStore +const { initIfNecessary } = walletStore const { wallet, signer } = storeToRefs(walletStore) const { newBookListing, updateEditionPrice } = bookStoreApiStore const { fetchStripeConnectStatusByWallet } = stripeStore @@ -823,7 +823,7 @@ async function submitNewClass () { let autoDeliverNFTsTxHash if (autoDeliverCount > 0) { if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('Unable to connect to wallet') diff --git a/pages/nft-book-store/send/[classId].vue b/pages/nft-book-store/send/[classId].vue index db47f4461..426ded3df 100644 --- a/pages/nft-book-store/send/[classId].vue +++ b/pages/nft-book-store/send/[classId].vue @@ -180,7 +180,7 @@ const { LIKE_CO_API, LCD_URL } = useRuntimeConfig().public const store = useWalletStore() const { wallet, signer } = storeToRefs(store) -const { connect } = store +const { initIfNecessary } = store const bookStoreApiStore = useBookStoreApiStore() const { token } = storeToRefs(bookStoreApiStore) @@ -293,7 +293,7 @@ async function fetchNextNFTId (count = 1) { nftIdError.value = '' isAutoFetchingNFTId.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!ownerWallet.value) { return } const { nfts } = await getNFTs({ @@ -320,7 +320,7 @@ async function onSendNFTStart () { try { isLoading.value = true if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { return } if (nftId.value) { diff --git a/pages/nft-book-store/status/[classId]/edit/[editionIndex].vue b/pages/nft-book-store/status/[classId]/edit/[editionIndex].vue index e19c59251..9d2a4505e 100644 --- a/pages/nft-book-store/status/[classId]/edit/[editionIndex].vue +++ b/pages/nft-book-store/status/[classId]/edit/[editionIndex].vue @@ -241,7 +241,7 @@ const { LIKE_CO_API } = useRuntimeConfig().public const walletStore = useWalletStore() const bookStoreApiStore = useBookStoreApiStore() -const { connect } = walletStore +const { initIfNecessary } = walletStore const { wallet, signer } = storeToRefs(walletStore) const { token } = storeToRefs(bookStoreApiStore) const { updateBookListingSetting } = bookStoreApiStore @@ -490,7 +490,7 @@ async function handleSubmit () { let autoDeliverNFTsTxHash if (newAutoDeliverNFTsCount > 0) { if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('Unable to connect to wallet') diff --git a/pages/nft-book-store/status/[classId]/edit/new.vue b/pages/nft-book-store/status/[classId]/edit/new.vue index 0e6213e99..e61144472 100644 --- a/pages/nft-book-store/status/[classId]/edit/new.vue +++ b/pages/nft-book-store/status/[classId]/edit/new.vue @@ -248,7 +248,7 @@ const { LIKE_CO_API } = useRuntimeConfig().public const walletStore = useWalletStore() const bookStoreApiStore = useBookStoreApiStore() -const { connect } = walletStore +const { initIfNecessary } = walletStore const { wallet, signer } = storeToRefs(walletStore) const { token } = storeToRefs(bookStoreApiStore) const { updateBookListingSetting } = bookStoreApiStore @@ -453,7 +453,7 @@ async function handleSubmit () { let autoDeliverNFTsTxHash if (editedPrice.isAutoDeliver && editedPrice.stock > 0) { if (!wallet.value || !signer.value) { - await connect() + await initIfNecessary() } if (!wallet.value || !signer.value) { throw new Error('Unable to connect to wallet') diff --git a/stores/book-store-api.ts b/stores/book-store-api.ts index 1745fb411..afe1e1b82 100644 --- a/stores/book-store-api.ts +++ b/stores/book-store-api.ts @@ -28,7 +28,7 @@ export const useBookStoreApiStore = defineStore('book-api', () => { clearAuthSession() } - async function restoreSession () { + async function restoreAuthSession () { try { isRestoringSession.value = true const session = loadAuthSession() @@ -36,7 +36,7 @@ export const useBookStoreApiStore = defineStore('book-api', () => { token.value = session.token sessionWallet.value = session.wallet if (session.wallet) { - await walletStore.connect() + await walletStore.restoreSession() } } } finally { @@ -121,7 +121,7 @@ export const useBookStoreApiStore = defineStore('book-api', () => { isAuthenticated, isRestoringSession, clearSession, - restoreSession, + restoreAuthSession, authenticate, newBookListing, updateBookListingSetting, diff --git a/stores/wallet.ts b/stores/wallet.ts index 2791d580b..ba7324884 100644 --- a/stores/wallet.ts +++ b/stores/wallet.ts @@ -80,25 +80,59 @@ export const useWalletStore = defineStore('wallet', () => { return con } - async function connect () { + async function initWallet (connection: LikeCoinWalletConnectorConnectionResult) { if (!connector.value) { connector.value = await initConnector() } - const session = connector.value.restoreSession() - let connection: LikeCoinWalletConnectorConnectionResult | null = null - if (session) { - try { - const result = await connector.value.initIfNecessary() - if (result) { connection = result } - } catch (e) { - console.error(e) + connector.value.once('account_change', async (currentMethod) => { + const latestConnection = await connector.value?.init(currentMethod) + if (latestConnection) { await initWallet(latestConnection) } + }) + await handleConnection(connection) + } + + async function initIfNecessary () { + if (!connector.value) { + connector.value = await initConnector() + } + const connection = await connector.value.initIfNecessary() + if (connection) { + await initWallet(connection) + } + } + + async function openConnectWalletModal () { + connector.value = await initConnector() + const connection = await connector.value.openConnectionMethodSelectionDialog() + return connection + } + + async function restoreSession () { + let hasSession = false + try { + if (window.localStorage) { + hasSession = !!window.localStorage.getItem( + 'likecoin_wallet_connector_session' + ) } + } catch (err) { + // eslint-disable-next-line no-console + console.error(err) } - if (!connection) { - const result = await connector.value.openConnectionMethodSelectionDialog() - if (result) { connection = result } + if (hasSession) { + if (!connector.value) { + connector.value = await initConnector() + } + const session = connector.value.restoreSession() + if (session) { + try { + const connection = await connector.value.initIfNecessary() + if (connection) { await initWallet(connection) } + } catch (e) { + console.error(e) + } + } } - if (connection) { await handleConnection(connection) } } function handleConnection (connection: LikeCoinWalletConnectorConnectionResult) { @@ -126,7 +160,7 @@ export const useWalletStore = defineStore('wallet', () => { async function signMessageMemo (action: string, permissions?: string[]) { if (!signer.value || !wallet.value) { - await connect() + await initIfNecessary() } if (!signer.value || !wallet.value) { throw new Error('WALLET_NOT_INITED') @@ -177,9 +211,14 @@ export const useWalletStore = defineStore('wallet', () => { signer, wallet, isConnected, - connect, disconnect, handleConnectorRedirect, - signMessageMemo + signMessageMemo, + initConnector, + + initWallet, + initIfNecessary, + openConnectWalletModal, + restoreSession } })