Skip to content

Commit

Permalink
Sign in with new user (testUser) composable working
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed Sep 12, 2023
1 parent 4f670be commit 3c05391
Show file tree
Hide file tree
Showing 15 changed files with 691 additions and 780 deletions.
8 changes: 4 additions & 4 deletions apps/web/src/composables/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useEnvironment from '@/composables/environment'
import { LoginCredentials } from '@casimir/types'
import { SignInWithEthereumCredentials } from '@casimir/types'

const { domain, origin, usersUrl } = useEnvironment()

Expand Down Expand Up @@ -45,17 +45,17 @@ export default function useAuth() {
* Signs user up if they don't exist, otherwise
* logs the user in with an address, message, and signed message
*
* @param {LoginCredentials} loginCredentials - The user's address, provider, currency, message, and signed message
* @param {SignInWithEthereumCredentials} signInWithEthereumCredentials - The user's address, provider, currency, message, and signed message
* @returns {Promise<Response>} - The response from the login request
*/
async function signInWithEthereum(loginCredentials: LoginCredentials): Promise<void> {
async function signInWithEthereum(signInWithEthereumCredentials: SignInWithEthereumCredentials): Promise<void> {
try {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(loginCredentials)
body: JSON.stringify(signInWithEthereumCredentials)
}
const response = await fetch(`${usersUrl}/auth/login`, requestOptions)
const { error, message } = await response.json()
Expand Down
313 changes: 2 additions & 311 deletions apps/web/src/composables/contracts.ts

Large diffs are not rendered by default.

41 changes: 1 addition & 40 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { EthersProvider } from '@casimir/types'
import { Account, TransactionRequest, UserWithAccountsAndOperators } from '@casimir/types'
import { GasEstimate, LoginCredentials, MessageRequest, ProviderString } from '@casimir/types'
import useAuth from '@/composables/auth'
import useContracts from '@/composables/contracts'
import useEnvironment from '@/composables/environment'
import useUsers from '@/composables/users'

interface ethereumWindow extends Window {
ethereum: any;
Expand All @@ -31,30 +29,6 @@ export default function useEthers() {
}
}

async function blockListener(blockNumber: number) {
const { manager, refreshBreakdown } = useContracts()
const { user } = useUsers()

console.log('blockNumber :>> ', blockNumber)
const addresses = (user.value as UserWithAccountsAndOperators).accounts.map((account: Account) => account.address) as string[]
const block = await provider.getBlockWithTransactions(blockNumber)

const txs = block.transactions.map(async (tx) => {
if (addresses.includes(tx.from.toLowerCase())) {
console.log('tx :>> ', tx)
try {
// const response = manager.interface.parseTransaction({ data: tx.data })
// console.log('response :>> ', response)
await refreshBreakdown()
} catch (error) {
console.error('Error parsing transaction:', error)
}
}
})

await Promise.all(txs)
}

/**
* Estimate gas fee using EIP 1559 methodology
* @returns string in ETH
Expand Down Expand Up @@ -175,13 +149,6 @@ export default function useEthers() {
return maxAfterFees
}

async function listenForTransactions() {
provider.on('block', blockListener as ethers.providers.Listener)
await new Promise(() => {
// Wait indefinitely using a Promise that never resolves
})
}

async function loginWithEthers(loginCredentials: LoginCredentials): Promise<void>{
const { provider, address, currency } = loginCredentials
const browserProvider = getBrowserProvider(provider)
Expand All @@ -190,7 +157,7 @@ export default function useEthers() {
const message = await createSiweMessage(address, 'Sign in with Ethereum to the app.')
const signer = web3Provider.getSigner()
const signedMessage = await signer.signMessage(message)
await signInWithEthereum({
await signInWithEthereum({
address,
currency,
message,
Expand Down Expand Up @@ -240,10 +207,6 @@ export default function useEthers() {
return signature
}

function stopListeningForTransactions() {
provider.off('block', blockListener as ethers.providers.Listener)
}

async function switchEthersNetwork (providerString: ProviderString, chainId: string) {
const provider = getBrowserProvider(providerString)
const currentChainId = await provider.networkVersion
Expand Down Expand Up @@ -282,12 +245,10 @@ export default function useEthers() {
getEthersBrowserSigner,
getGasPriceAndLimit,
getMaxETHAfterFees,
listenForTransactions,
loginWithEthers,
requestEthersAccount,
sendEthersTransaction,
signEthersMessage,
stopListeningForTransactions,
switchEthersNetwork
}
}
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/composables/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ export default function useFormat() {
const scale = Math.pow(10, tier * 3)
const scaled = number / scale
return scaled.toFixed(2) + suffix
}
}

function trimAndLowercaseAddress(address: string) {
return address.trim().toLowerCase()
}

return {
convertString,
formatDecimalString,
formatNumber
formatNumber,
trimAndLowercaseAddress
}
}
19 changes: 0 additions & 19 deletions apps/web/src/composables/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createWebHistory, createRouter } from 'vue-router'
import useUsers from '@/composables/users'
import Overview from '@/pages/overview/Overview.vue'
import Operator from '@/pages/operators/Operator.vue'

Expand Down Expand Up @@ -31,22 +30,4 @@ const router = createRouter({
routes
})

// TO DO: Add a routing beforeEach that
// dynamically fixes rerouting to auth page

router.beforeEach(async (to, from, next) => {
const { checkUserSessionExists } = useUsers()
await checkUserSessionExists()
next()
// if (to.fullPath === '/auth' && !loggedIn) {
// next()
// } else if (to.fullPath === '/auth' && loggedIn) {
// next('/')
// } else if (!loggedIn) {
// next('/auth')
// } else {
// next()
// }
})

export default router
Loading

0 comments on commit 3c05391

Please sign in to comment.