-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📱 Add sign in button for mobile (#245)
* 📱 Add sign in button for mobile * 🚸 Disable sales link copy for non-affiliates * ♻️ Refactor user sign in logic into composables * 🎨 Rename props
- Loading branch information
1 parent
8f126a1
commit b76d25b
Showing
4 changed files
with
87 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ref } from 'vue' | ||
import { storeToRefs } from 'pinia' | ||
import { useWalletStore } from '~/stores/wallet' | ||
import { useBookStoreApiStore } from '~/stores/book-store-api' | ||
|
||
export function useAuth () { | ||
const bookStoreApiStore = useBookStoreApiStore() | ||
const store = useWalletStore() | ||
const { wallet, signer } = storeToRefs(store) | ||
const { connect, disconnect, signMessageMemo } = store | ||
const { authenticate, clearSession } = bookStoreApiStore | ||
const toast = useToast() | ||
|
||
const isAuthenticating = ref(false) | ||
|
||
async function onAuthenticate () { | ||
try { | ||
isAuthenticating.value = true | ||
setupPostAuthRedirect() | ||
|
||
if (!wallet.value || !signer.value) { | ||
await connect() | ||
} | ||
if (!wallet.value || !signer.value) { | ||
return | ||
} | ||
|
||
const signature = await signMessageMemo( | ||
'authorize', | ||
['read:nftbook', 'write:nftbook', 'read:nftcollection', 'write:nftcollection'] | ||
) | ||
|
||
if (!signature) { | ||
return | ||
} | ||
|
||
await authenticate(wallet.value, signature) | ||
} catch (err) { | ||
disconnect() | ||
clearSession() | ||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
toast.add({ | ||
icon: 'i-heroicons-exclamation-circle', | ||
title: (err as Error).toString(), | ||
timeout: 0, | ||
color: 'red', | ||
ui: { | ||
title: 'text-red-400 dark:text-red-400' | ||
} | ||
}) | ||
} finally { | ||
isAuthenticating.value = false | ||
clearPostAuthRedirect() | ||
} | ||
} | ||
|
||
return { | ||
isAuthenticating, | ||
onAuthenticate | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters