-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e42196
commit 5d5a8b2
Showing
9 changed files
with
159 additions
and
30 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
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
<BaseFormGroup label="Use your e-mail" label-class="text-opaque"> | ||
<b-form-input v-model="email" placeholder="[email protected]" /> | ||
</BaseFormGroup> | ||
<b-button :disabled="!isEmailValid" variant="primary" type="submit" class="w-100"> | ||
<b-button :disabled="!isEmailValid || isLoadingOTP" variant="primary" type="submit" class="w-100"> | ||
<b-spinner v-if="isLoadingOTP" small /> | ||
<template v-else> | ||
Send one-time password | ||
|
@@ -30,7 +30,7 @@ | |
<BaseFormGroup label="Check your e-mail for the OTP"> | ||
<b-form-input v-model="otp" placeholder="******" /> | ||
</BaseFormGroup> | ||
<b-button :disabled="!isOTPValid" variant="primary" type="submit" class="w-100"> | ||
<b-button :disabled="!isOTPValid || isLoadingOTPVerify" variant="primary" type="submit" class="w-100"> | ||
<b-spinner v-if="isLoadingOTPVerify" small /> | ||
<template v-else> | ||
Verify OTP | ||
|
@@ -41,6 +41,34 @@ | |
|
||
<BaseHrOrSeparator /> | ||
|
||
<b-button | ||
v-if="!isWalletConnected" | ||
:disabled="isLoadingConnectWallet" | ||
variant="primary" | ||
class="w-100" | ||
@click="onClickSigninConnectWallet" | ||
> | ||
<b-spinner v-if="isLoadingConnectWallet" small /> | ||
<template v-else> | ||
Connect Wallet | ||
<BaseIcon icon="chevron-right" class="ms-2" /> | ||
</template> | ||
</b-button> | ||
<b-button | ||
v-else | ||
:disabled="isLoadingSigninWallet" | ||
variant="success" | ||
class="w-100" | ||
@click="onClickSigninWithWallet" | ||
> | ||
<b-spinner v-if="isLoadingSigninWallet" small /> | ||
<template v-else> | ||
Continue with <strong>{{ walletStore.account && shortenAddress(walletStore.account.address) }}</strong> | ||
<BaseIcon icon="chevron-right" class="ms-2" /> | ||
</template> | ||
</b-button> | ||
|
||
<BaseHrOrSeparator /> | ||
<BaseFormGroup label="Use a trusted provider" label-class="text-opaque"> | ||
<div class="d-flex justify-content-between w-100 gap-2"> | ||
<b-button | ||
|
@@ -59,10 +87,12 @@ | |
</template> | ||
|
||
<script lang="ts"> | ||
import { AccessTokenKind, AccountVariant } from '@thxnetwork/common/enums'; | ||
import { useAuthStore, useWalletStore } from '@thxnetwork/wallet/stores'; | ||
import { toast } from '@thxnetwork/wallet/utils/toast'; | ||
import { mapStores } from 'pinia'; | ||
import { defineComponent } from 'vue'; | ||
import { useAuthStore } from '@thxnetwork/wallet/stores'; | ||
import { AccessTokenKind, AccountVariant } from '@thxnetwork/common/enums'; | ||
import { shortenAddress } from '../utils/address'; | ||
export default defineComponent({ | ||
name: 'BaseModalLogin', | ||
|
@@ -98,29 +128,72 @@ export default defineComponent({ | |
title: 'Sign in with Github', | ||
}, | ||
} as any, | ||
isLoadingConnectWallet: false, | ||
isLoadingSigninWallet: false, | ||
isLoadingProvider: null as null | AccountVariant, | ||
isLoadingOTP: false, | ||
isLoadingOTPVerify: false, | ||
isEmailSent: false, | ||
shortenAddress, | ||
}; | ||
}, | ||
computed: { | ||
...mapStores(useAuthStore), | ||
...mapStores(useAuthStore, useWalletStore), | ||
isEmailValid() { | ||
return this.email.length > 0; | ||
}, | ||
isOTPValid() { | ||
return this.otp.length === 6; | ||
}, | ||
isWalletConnected() { | ||
return this.walletStore.account && this.walletStore.account.isConnected; | ||
}, | ||
}, | ||
methods: { | ||
async onClickSigninConnectWallet() { | ||
try { | ||
this.isLoadingConnectWallet = true; | ||
await this.walletStore.disconnect(); | ||
await this.walletStore.connect(); | ||
} catch (error: any) { | ||
this.onError(error); | ||
} finally { | ||
this.isLoadingConnectWallet = false; | ||
} | ||
}, | ||
async onClickSigninWithWallet() { | ||
try { | ||
this.isLoadingSigninWallet = true; | ||
const address = this.walletStore.account?.address as `0x${string}`; | ||
const message = 'This signature will be used to proof ownership of a web3 account.'; | ||
const signature = await this.walletStore.signMessage(message); | ||
await this.authStore.signinWithWallet(address, { message, signature }); | ||
} catch (error: any) { | ||
const { code, details, message } = error; | ||
// Indicates connect state in w3modal but not connected in wallet | ||
if (message === 'connection.connector.getProvider is not a function') { | ||
this.onError(new Error('No wallet connection, please disconnect and try again.')); | ||
} | ||
// Other exceptions are checked for a code to be present, indicating metamask errors | ||
else { | ||
this.onError(new Error(code ? details : message)); | ||
} | ||
} finally { | ||
this.isLoadingSigninWallet = false; | ||
} | ||
}, | ||
async onSubmitSigninWithOTP() { | ||
this.isLoadingOTP = true; | ||
try { | ||
await this.authStore.signInWithOtp({ email: this.email }); | ||
this.isEmailSent = true; | ||
} catch (error) { | ||
this.error = (error as Error).message; | ||
} catch (error: any) { | ||
this.onError(error); | ||
} finally { | ||
this.isLoadingOTP = false; | ||
} | ||
|
@@ -130,8 +203,8 @@ export default defineComponent({ | |
try { | ||
await this.authStore.verifyOtp({ email: this.email, token: this.otp }); | ||
if (!this.authStore.session) throw new Error('An issue occured while verifying OTP. Please try again.'); | ||
} catch (error) { | ||
this.error = (error as Error).message; | ||
} catch (error: any) { | ||
this.onError(error); | ||
} finally { | ||
this.isLoadingOTPVerify = false; | ||
} | ||
|
@@ -140,11 +213,16 @@ export default defineComponent({ | |
this.isLoadingProvider = this.providers[variant].kind; | ||
try { | ||
await this.authStore.signInWithOAuth({ variant }); | ||
} catch (error) { | ||
this.error = (error as Error).message; | ||
} catch (error: any) { | ||
this.onError(error); | ||
this.isLoadingProvider = null; | ||
} | ||
}, | ||
onError(error: Error) { | ||
toast(error.message, 'light', 3000, () => { | ||
return; | ||
}); | ||
}, | ||
}, | ||
}); | ||
</script> |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export function shortenAddress(address: string) { | ||
export function shortenAddress(address: `0x${string}` | undefined) { | ||
if (!address) return ''; | ||
return `${address.substring(0, 5)}...${address.substring(address.length - 5, address.length)}`; | ||
} |