Skip to content

Commit

Permalink
Merge pull request #608 from 1ifeworld/0xTranqui/auth-backup
Browse files Browse the repository at this point in the history
add backup fetch for userid in user contexts
  • Loading branch information
0xTranqui authored Apr 30, 2024
2 parents f7de4a8 + ada87e3 commit afcf52a
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions apps/site/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import React, {
useState,
} from 'react'
import { getUserDataByOwner } from '@/lib'
import { getUserId } from '@/gql'
import { Address } from 'viem'

const UserContext = createContext<{
embeddedWallet?: ConnectedWallet
Expand Down Expand Up @@ -46,15 +48,24 @@ export function UserContextComponent({ children }: { children: ReactNode }) {
async function fetchUserData() {
if (!embeddedWallet) return

// try to fetch data from username db
const data = await getUserDataByOwner({ owner: embeddedWallet.address })

if (!data) return

setUserId(BigInt(data.records.id))
setUsername(data.records.name)

const userChannels = await getUserChannels(data.records.id)
if (userChannels) setUserChannels(userChannels)
let userIdFromDelta
if (data) {
// if succcessful from username db, set user id, name, and channels for user
setUserId(BigInt(data.records.id))
setUsername(data.records.name)
const userChannels = await getUserChannels(data.records.id)
if (userChannels) setUserChannels(userChannels)
} else {
// if not successful from username db, fetch from ponder. means that user may have id
// but not username yet
userIdFromDelta = await getUserId({
custodyAddress: embeddedWallet.address as Address,
})
if (!userIdFromDelta.userId) return
setUserId(userIdFromDelta.userId)
}
}

function clearUserData() {
Expand Down

0 comments on commit afcf52a

Please sign in to comment.