Skip to content

Commit

Permalink
♻️ Split connect method into smaller functions (#270)
Browse files Browse the repository at this point in the history
* ♻️ Split connect method into smaller functions

* 🎨 Update restoreSession to restoreAuthSession

* ♻️ Refactor authentication flow with openConnectWalletModal and initWallet

* ♻️ Replace connect method with initIfNecessary for wallet initialization
  • Loading branch information
AuroraHuang22 authored Feb 3, 2025
1 parent a76a3ec commit e0ef48d
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 46 deletions.
5 changes: 3 additions & 2 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -91,7 +92,7 @@ useSeoMeta({
})
onMounted(async () => {
await restoreSession()
await restoreAuthSession()
})
</script>
11 changes: 6 additions & 5 deletions composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions pages/mint-nft/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') }
Expand Down Expand Up @@ -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') }
Expand Down Expand Up @@ -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') }
Expand Down
4 changes: 2 additions & 2 deletions pages/nft-book-store/collection/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions pages/nft-book-store/collection/send/[collectionId].vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions pages/nft-book-store/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions pages/nft-book-store/send/[classId].vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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({
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pages/nft-book-store/status/[classId]/edit/[editionIndex].vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions pages/nft-book-store/status/[classId]/edit/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions stores/book-store-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export const useBookStoreApiStore = defineStore('book-api', () => {
clearAuthSession()
}

async function restoreSession () {
async function restoreAuthSession () {
try {
isRestoringSession.value = true
const session = loadAuthSession()
if (session) {
token.value = session.token
sessionWallet.value = session.wallet
if (session.wallet) {
await walletStore.connect()
await walletStore.restoreSession()
}
}
} finally {
Expand Down Expand Up @@ -121,7 +121,7 @@ export const useBookStoreApiStore = defineStore('book-api', () => {
isAuthenticated,
isRestoringSession,
clearSession,
restoreSession,
restoreAuthSession,
authenticate,
newBookListing,
updateBookListingSetting,
Expand Down
71 changes: 55 additions & 16 deletions stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -177,9 +211,14 @@ export const useWalletStore = defineStore('wallet', () => {
signer,
wallet,
isConnected,
connect,
disconnect,
handleConnectorRedirect,
signMessageMemo
signMessageMemo,
initConnector,

initWallet,
initIfNecessary,
openConnectWalletModal,
restoreSession
}
})

0 comments on commit e0ef48d

Please sign in to comment.