Skip to content

Commit a5233b9

Browse files
add: better UX flow
1 parent 1333715 commit a5233b9

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

src/components/Page/Index.vue

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
hasWritePermission,
2626
} from '../../fixtures/memberships'
2727
import { JsonRpcProvider } from 'ethers'
28-
import { getSignature, getMessage, consoleWarn } from '../../fixtures/session'
28+
import { getSignature, getMessage, checkSession, consoleWarn } from '../../fixtures/session'
2929
import { Strings } from '../../i18n'
3030
3131
type Props = {
@@ -123,20 +123,15 @@ const handleConnection = async (signer: UndefinedOr<Signer>) => {
123123
const connectedAddress = signer
124124
? await signer.getAddress()
125125
: await newSigner.getAddress()
126-
// const connectedAddress = '0x57E21bd98612DE0Bd1723F4bf81A944eF7BfF526'
126+
127127
walletAddress.value = connectedAddress
128-
console.log({ connectedAddress })
129128
130-
const hash = getMessage(connectedAddress)
131-
132-
let sig = await getSignature(connectedAddress, signer)
133-
134-
fetchPosts({ hash, sig })
135-
136-
hasEditableRole.value = await testPermission(
137-
walletAddress.value,
138-
new JsonRpcProvider(props.rpcUrl),
139-
)
129+
if(isVerified.value){
130+
hasEditableRole.value = await testPermission(
131+
walletAddress.value,
132+
new JsonRpcProvider(props.rpcUrl),
133+
)
134+
}
140135
}
141136
142137
const fetchPosts = async ({ hash, sig }: { hash?: string; sig?: string }) => {
@@ -182,32 +177,32 @@ onMounted(async () => {
182177
'@devprotocol/clubs-core/connection'
183178
)
184179
connection.value = conct
185-
conct().signer.subscribe((signer: UndefinedOr<Signer>) => {
186-
walletSigner = signer
187-
})
188-
const signer = conct().signer.value
189-
console.log({ signer })
190-
console.log({ walletSigner })
191-
if (signer) {
180+
conct().signer.subscribe(async(signer: UndefinedOr<Signer>) => {
181+
if (signer) {
192182
const connectedAddress = await signer.getAddress()
193183
walletAddress.value = connectedAddress
194-
isVerified.value = true
195-
}
196-
setTimeout(() => {
197-
handleConnection(walletSigner)
198-
}, 1000)
199-
184+
isVerified.value = await checkSession(connectedAddress)
185+
console.log( "isverfied", isVerified.value )
186+
walletSigner = signer
187+
handleConnection(signer)
188+
}
189+
})
200190
i18n.value = i18nBase(navigator.languages)
201191
})
202192
203193
const isVerified = ref(false)
204194
205195
const handleVerify = async () => {
206-
handleConnection(walletSigner)
196+
const walletAddres = await walletSigner?.getAddress() as string
197+
const hash = getMessage(walletAddres)
198+
let sig = await getSignature(walletAddres, walletSigner as Signer)
199+
fetchPosts({ hash, sig })
207200
//
208201
if (connection.value?.().signer.value) {
209202
isVerified.value = true
210203
}
204+
205+
handleConnection(walletSigner)
211206
}
212207
213208
const handlePostSuccess = (post: Posts) => {

src/fixtures/session.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type UndefinedOr } from '@devprotocol/util-ts'
33
import { type Signer } from 'ethers'
44
import { encode, decode } from '@devprotocol/clubs-core'
55
import { verifyMessage } from 'ethers'
6+
import type { Address } from 'cluster'
67

78
const maxValidity = 1 * 60 * 60 * 1000 // 1 hour
89

@@ -40,10 +41,39 @@ export const getSignature = async (
4041
}
4142

4243
export const getSessionAddress = async (hash: string, sig: string) => {
44+
console.log('inside getSessionAddress')
4345
const address = verifyMessage(hash, sig)
4446
return address
4547
}
4648

49+
export const checkSession = async (address: string) => {
50+
console.log('inside checkSession')
51+
const hash = sessionStorage.getItem(`hash-of-${address}`)
52+
const sigItem = sessionStorage.getItem(`sig-of-${address}`);
53+
const sig = sigItem ? decode(sigItem) as string : undefined;
54+
console.log({hash, sig})
55+
56+
if (!hash || !sig) {
57+
return false;
58+
}
59+
60+
try{
61+
const SessoionAddress = await getSessionAddress(hash, sig)
62+
console.log({SessoionAddress, address})
63+
64+
if (SessoionAddress !== address) {
65+
console.log('inside here')
66+
return false
67+
}
68+
return true
69+
}
70+
catch(error) {
71+
console.log('inside catch')
72+
console.error('Error checking session:', error);
73+
return false
74+
}
75+
}
76+
4777
export const consoleWarn = () => {
4878
console.log('%cWarning!!', 'color: red; font-size: 20px; background: yellow;')
4979
console.log(

0 commit comments

Comments
 (0)