Skip to content

Commit

Permalink
fix: correctly show users identities when visiting their profile
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Jan 9, 2024
1 parent a8637d3 commit 99202b2
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 26 deletions.
6 changes: 5 additions & 1 deletion api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type Query {
appConfig: AppConfig!
me: User
uptime: Float!
userFindManyIdentity: [Identity!]
userFindManyIdentity(input: UserFindManyIdentityInput!): [Identity!]
userFindManyUser(input: UserFindManyUserInput!): UserPaging!
userFindOneUser(username: String!): User
userRequestIdentityChallenge(input: RequestIdentityChallengeInput!): IdentityChallenge
Expand Down Expand Up @@ -163,6 +163,10 @@ type User {
username: String
}

input UserFindManyIdentityInput {
username: String!
}

input UserFindManyUserInput {
limit: Int = 10
page: Int = 1
Expand Down
1 change: 1 addition & 0 deletions libs/api/identity/data-access/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './lib/dto/admin-create-identity.input'
export * from './lib/dto/admin-find-many-identity.input'
export * from './lib/dto/link-identity-input'
export * from './lib/dto/request-identity-challenge.input'
export * from './lib/dto/user-find-many-identity-input'
export * from './lib/dto/verify-identity-challenge-input'
export * from './lib/entity/identity-challenge.entity'
export * from './lib/entity/identity-provider.enum'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { verifySignature } from '@pubkeyapp/solana-verify-wallet'
import { ApiSolanaIdentityService } from './api-solana-identity.service'
import { LinkIdentityInput } from './dto/link-identity-input'
import { RequestIdentityChallengeInput } from './dto/request-identity-challenge.input'
import { UserFindManyIdentityInput } from './dto/user-find-many-identity-input'
import { VerifyIdentityChallengeInput } from './dto/verify-identity-challenge-input'
import { sha256 } from './helpers/sha256'

Expand All @@ -31,9 +32,9 @@ export class ApiUserIdentityService {
return true
}

async findManyIdentity(userId: string): Promise<PrismaIdentity[]> {
async findManyIdentity(input: UserFindManyIdentityInput): Promise<PrismaIdentity[]> {
const items = await this.core.data.identity.findMany({
where: { ownerId: userId },
where: { owner: { username: input.username } },
orderBy: [{ provider: 'asc' }, { providerId: 'asc' }],
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Field, InputType } from '@nestjs/graphql'

@InputType()
export class UserFindManyIdentityInput {
@Field()
username!: string
}
14 changes: 7 additions & 7 deletions libs/api/identity/feature/src/lib/api-user-identity.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ import {
IdentityChallenge,
LinkIdentityInput,
RequestIdentityChallengeInput,
UserFindManyIdentityInput,
VerifyIdentityChallengeInput,
} from '@pubkey-stack/api-identity-data-access'
import { User } from '@pubkey-stack/api-user-data-access'

@Resolver()
@UseGuards(ApiAuthGraphQLUserGuard)
export class ApiUserIdentityResolver {
constructor(private readonly service: ApiIdentityService) {}

@Mutation(() => Boolean, { nullable: true })
userDeleteIdentity(@CtxUser() user: User, @Args('identityId') identityId: string) {
userDeleteIdentity(@CtxUser() user: { id: string }, @Args('identityId') identityId: string) {
return this.service.user.deleteIdentity(user.id, identityId)
}

@Query(() => IdentityChallenge, { nullable: true })
userRequestIdentityChallenge(
@Context() ctx: BaseContext,
@CtxUser() user: User,
@CtxUser() user: { id: string },
@Args('input') input: RequestIdentityChallengeInput,
) {
return this.service.user.requestIdentityChallenge(ctx, user.id, input)
}

@Mutation(() => Identity, { nullable: true })
userLinkIdentity(@CtxUser() user: User, @Args('input') input: LinkIdentityInput) {
userLinkIdentity(@CtxUser() user: { id: string }, @Args('input') input: LinkIdentityInput) {
return this.service.user.linkIdentity(user.id, input)
}

@Mutation(() => IdentityChallenge, { nullable: true })
userVerifyIdentityChallenge(
@Context() ctx: BaseContext,
@CtxUser() user: User,
@CtxUser() user: { id: string },
@Args('input') input: VerifyIdentityChallengeInput,
) {
return this.service.user.verifyIdentityChallenge(ctx, user.id, input)
}

@Query(() => [Identity], { nullable: true })
userFindManyIdentity(@CtxUser() user: User) {
return this.service.user.findManyIdentity(user.id)
userFindManyIdentity(@Args('input') input: UserFindManyIdentityInput) {
return this.service.user.findManyIdentity(input)
}
}
1 change: 1 addition & 0 deletions libs/api/user/data-access/src/lib/api-user-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class ApiUserUserService {
}

async findOneUser(username: string) {
console.log(`username`, username)
const found = await this.core.data.user.findUnique({ where: { username } })

if (!found) {
Expand Down
24 changes: 20 additions & 4 deletions libs/sdk/src/generated/graphql-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export type QueryAnonRequestIdentityChallengeArgs = {
input: RequestIdentityChallengeInput
}

export type QueryUserFindManyIdentityArgs = {
input: UserFindManyIdentityInput
}

export type QueryUserFindManyUserArgs = {
input: UserFindManyUserInput
}
Expand Down Expand Up @@ -259,6 +263,10 @@ export type User = {
username?: Maybe<Scalars['String']['output']>
}

export type UserFindManyIdentityInput = {
username: Scalars['String']['input']
}

export type UserFindManyUserInput = {
limit?: InputMaybe<Scalars['Int']['input']>
page?: InputMaybe<Scalars['Int']['input']>
Expand Down Expand Up @@ -503,7 +511,9 @@ export type AdminDeleteIdentityMutationVariables = Exact<{

export type AdminDeleteIdentityMutation = { __typename?: 'Mutation'; deleted?: boolean | null }

export type UserFindManyIdentityQueryVariables = Exact<{ [key: string]: never }>
export type UserFindManyIdentityQueryVariables = Exact<{
input: UserFindManyIdentityInput
}>

export type UserFindManyIdentityQuery = {
__typename?: 'Query'
Expand Down Expand Up @@ -975,8 +985,8 @@ export const AdminDeleteIdentityDocument = gql`
}
`
export const UserFindManyIdentityDocument = gql`
query userFindManyIdentity {
items: userFindManyIdentity {
query userFindManyIdentity($input: UserFindManyIdentityInput!) {
items: userFindManyIdentity(input: $input) {
...IdentityDetails
}
}
Expand Down Expand Up @@ -1296,7 +1306,7 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
)
},
userFindManyIdentity(
variables?: UserFindManyIdentityQueryVariables,
variables: UserFindManyIdentityQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<{
data: UserFindManyIdentityQuery
Expand Down Expand Up @@ -1701,6 +1711,12 @@ export function RequestIdentityChallengeInputSchema(): z.ZodObject<Properties<Re
})
}

export function UserFindManyIdentityInputSchema(): z.ZodObject<Properties<UserFindManyIdentityInput>> {
return z.object({
username: z.string(),
})
}

export function UserFindManyUserInputSchema(): z.ZodObject<Properties<UserFindManyUserInput>> {
return z.object({
limit: z.number().nullish(),
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk/src/graphql/feature-identity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ mutation adminDeleteIdentity($identityId: String!) {
deleted: adminDeleteIdentity(identityId: $identityId)
}

query userFindManyIdentity {
items: userFindManyIdentity {
query userFindManyIdentity($input: UserFindManyIdentityInput!) {
items: userFindManyIdentity(input: $input) {
...IdentityDetails
}
}
Expand Down
4 changes: 3 additions & 1 deletion libs/web/dev/feature/src/lib/dev-identity-wizard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Grid } from '@mantine/core'
import { IdentityProvider } from '@pubkey-stack/sdk'
import { useAuth } from '@pubkey-stack/web-auth-data-access'
import { useCreateSignature, useUserFindManyIdentity } from '@pubkey-stack/web-identity-data-access'
import { IdentityUiSolanaWizard, IdentityUiSolanaWizardModal } from '@pubkey-stack/web-identity-ui'
import { toastError, toastSuccess, UiCard, UiDebug, UiStack } from '@pubkey-ui/core'
Expand All @@ -8,7 +9,8 @@ import { useWallet } from '@solana/wallet-adapter-react'
import { useCallback } from 'react'

export function DevIdentityWizard() {
const { items } = useUserFindManyIdentity()
const { user } = useAuth()
const { items } = useUserFindManyIdentity({ username: user?.username as string })
const { publicKey } = useWallet()
const challenge = 'Sign this message to verify your wallet'
const createSignature = useCreateSignature()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Identity, IdentityProvider } from '@pubkey-stack/sdk'
import { Identity, IdentityProvider, type UserFindManyIdentityInput } from '@pubkey-stack/sdk'
import { useSdk } from '@pubkey-stack/web-core-data-access'
import { toastError, toastSuccess } from '@pubkey-ui/core'
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'

export function useUserFindManyIdentity() {
export function useUserFindManyIdentity({ username }: { username: string }) {
const sdk = useSdk()
const input: UserFindManyIdentityInput = useMemo(() => ({ username }), [username])
const query = useQuery({
queryKey: ['user', 'find-many-identity'],
queryFn: () => sdk.userFindManyIdentity().then((res) => res?.data),
queryKey: ['user', 'find-many-identity', input],
queryFn: () => sdk.userFindManyIdentity({ input }).then((res) => res?.data),
})

const grouped: { provider: IdentityProvider; items: Identity[] }[] = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useAuth } from '@pubkey-stack/web-auth-data-access'
import { useUserFindManyIdentity } from '@pubkey-stack/web-identity-data-access'
import { IdentityUiGroupList } from '@pubkey-stack/web-identity-ui'
import { UiLoader, UiStack } from '@pubkey-ui/core'

export function SettingsIdentityFeature() {
const { deleteIdentity, grouped, query } = useUserFindManyIdentity()
const { user } = useAuth()
const { deleteIdentity, grouped, query } = useUserFindManyIdentity({ username: user?.username as string })

return (
<UiStack>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSdk } from '@pubkey-stack/web-core-data-access'
import { useQuery } from '@tanstack/react-query'

export function useUserFineOneUser(username: string) {
export function useUserFineOneUser({ username }: { username: string }) {
const sdk = useSdk()
const query = useQuery({
queryKey: ['user', 'find-one-user', username],
Expand Down
2 changes: 1 addition & 1 deletion libs/web/user/data-access/src/lib/use-user-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function useUserProfile() {
const sdk = useSdk()
const me = useMe(sdk)
const { user } = useAuth()
const { query } = useUserFineOneUser(user?.username as string)
const { query } = useUserFineOneUser({ username: user?.username as string })

return {
user: query.data?.item,
Expand Down
4 changes: 2 additions & 2 deletions libs/web/user/feature/src/lib/user-detail-feature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { Link, useParams } from 'react-router-dom'
export function UserDetailFeature() {
const { user: authUser } = useAuth()
const { username } = useParams<{ username: string }>() as { username: string }
const { user, query } = useUserFineOneUser(username)
const { items } = useUserFindManyIdentity()
const { user, query } = useUserFineOneUser({ username })
const { items } = useUserFindManyIdentity({ username })

if (query.isLoading) {
return <UiLoader />
Expand Down

0 comments on commit 99202b2

Please sign in to comment.