Skip to content

Commit

Permalink
Merge pull request #998 from dev-protocol/fix/signin
Browse files Browse the repository at this point in the history
Fix: Frequent signin required to prove admin rights
  • Loading branch information
KukretiShubham authored Jun 7, 2024
2 parents ffe33b2 + e7d2d1b commit af2dc61
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devprotocol/clubs-plugin-posts",
"version": "0.20.11",
"version": "0.20.12",
"type": "module",
"description": "Template repository for using TypeScript",
"main": "dist/index.js",
Expand Down
65 changes: 40 additions & 25 deletions src/components/Page/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import {
hasWritePermission,
} from '../../fixtures/memberships'
import { JsonRpcProvider } from 'ethers'
import { getSignature, getMessage, consoleWarn } from '../../fixtures/session'
import {
getSignature,
getMessage,
checkSession,
consoleWarn,
} from '../../fixtures/session'
import { Strings } from '../../i18n'
type Props = {
Expand Down Expand Up @@ -108,25 +113,30 @@ const testPermission = async (
}
const handleConnection = async (signer: UndefinedOr<Signer>) => {
let newSigner: Signer = signer as Signer
console.log({ signer })
if (!signer) {
return
const { connection: conct } = await import(
'@devprotocol/clubs-core/connection'
)
connection.value = conct
newSigner = conct().signer.value
console.log({ newSigner })
}
console.log('inside Handle Connection')
// get wallet address
const connectedAddress = await signer.getAddress()
// const connectedAddress = '0x57E21bd98612DE0Bd1723F4bf81A944eF7BfF526'
walletAddress.value = connectedAddress
const hash = getMessage(connectedAddress)
const connectedAddress = signer
? await signer.getAddress()
: await newSigner.getAddress()
let sig = await getSignature(connectedAddress, signer)
fetchPosts({ hash, sig })
walletAddress.value = connectedAddress
hasEditableRole.value = await testPermission(
walletAddress.value,
new JsonRpcProvider(props.rpcUrl),
)
if (isVerified.value) {
hasEditableRole.value = await testPermission(
walletAddress.value,
new JsonRpcProvider(props.rpcUrl),
)
}
}
const fetchPosts = async ({ hash, sig }: { hash?: string; sig?: string }) => {
Expand Down Expand Up @@ -172,27 +182,32 @@ onMounted(async () => {
'@devprotocol/clubs-core/connection'
)
connection.value = conct
conct().signer.subscribe((signer: UndefinedOr<Signer>) => {
walletSigner = signer
conct().signer.subscribe(async (signer: UndefinedOr<Signer>) => {
if (signer) {
const connectedAddress = await signer.getAddress()
walletAddress.value = connectedAddress
isVerified.value = await checkSession(connectedAddress)
console.log('isverfied', isVerified.value)
walletSigner = signer
handleConnection(signer)
}
})
const signer = conct().signer.value
if (signer) {
const connectedAddress = await signer.getAddress()
walletAddress.value = connectedAddress
isVerified.value = true
}
i18n.value = i18nBase(navigator.languages)
})
const isVerified = ref(false)
const handleVerify = async () => {
handleConnection(walletSigner)
const walletAddres = (await walletSigner?.getAddress()) as string
const hash = getMessage(walletAddres)
let sig = await getSignature(walletAddres, walletSigner as Signer)
fetchPosts({ hash, sig })
//
if (connection.value?.().signer.value) {
isVerified.value = true
}
handleConnection(walletSigner)
}
const handlePostSuccess = (post: Posts) => {
Expand Down
35 changes: 35 additions & 0 deletions src/fixtures/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { type UndefinedOr } from '@devprotocol/util-ts'
import { type Signer } from 'ethers'
import { encode, decode } from '@devprotocol/clubs-core'
import { verifyMessage } from 'ethers'
import type { Address } from 'cluster'

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

Expand Down Expand Up @@ -38,6 +40,39 @@ export const getSignature = async (
return decode(sig) as string
}

export const getSessionAddress = async (hash: string, sig: string) => {
console.log('inside getSessionAddress')
const address = verifyMessage(hash, sig)
return address
}

export const checkSession = async (address: string) => {
console.log('inside checkSession')
const hash = sessionStorage.getItem(`hash-of-${address}`)
const sigItem = sessionStorage.getItem(`sig-of-${address}`)
const sig = sigItem ? (decode(sigItem) as string) : undefined
console.log({ hash, sig })

if (!hash || !sig) {
return false
}

try {
const SessoionAddress = await getSessionAddress(hash, sig)
console.log({ SessoionAddress, address })

if (SessoionAddress !== address) {
console.log('inside here')
return false
}
return true
} catch (error) {
console.log('inside catch')
console.error('Error checking session:', error)
return false
}
}

export const consoleWarn = () => {
console.log('%cWarning!!', 'color: red; font-size: 20px; background: yellow;')
console.log(
Expand Down

0 comments on commit af2dc61

Please sign in to comment.