Skip to content

Commit

Permalink
fix: double-check user profile in db (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnugent authored Nov 16, 2024
1 parent b190931 commit 46df23c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/js/auth/initializeUserInDb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getClient } from '../graphql/ServerClient'
import { MUTATION_UPDATE_PROFILE } from '../graphql/gql/users'
import { MUTATION_UPDATE_PROFILE, QUERY_DOES_USERNAME_EXIST } from '../graphql/gql/users'
import { updateUser } from './ManagementClient'

export interface UpdateUsernameInput {
Expand All @@ -14,9 +14,15 @@ interface InitializeUserInDBParams extends UpdateUsernameInput {
auth0UserId: string
}

const serverClient = getClient()

export const initializeUserInDB = async (params: InitializeUserInDBParams): Promise<boolean> => {
const { auth0UserId, accessToken, userUuid, username, email, avatar } = params
const res = await getClient().mutate<{ updateUserProfile?: boolean }, UpdateUsernameInput>({
const existed = await doesUserExist(username)
if (existed) {
return false
}
const res = await serverClient.mutate<{ updateUserProfile?: boolean }, UpdateUsernameInput>({
mutation: MUTATION_UPDATE_PROFILE,
variables: {
userUuid,
Expand All @@ -42,3 +48,14 @@ export const initializeUserInDB = async (params: InitializeUserInDBParams): Prom
}
return success
}

const doesUserExist = async (username: string): Promise<boolean> => {
const res = await serverClient.query<{ usernameExists: boolean }, { username: string }>({
query: QUERY_DOES_USERNAME_EXIST,
variables: {
username
},
fetchPolicy: 'no-cache'
})
return res.data.usernameExists
}

0 comments on commit 46df23c

Please sign in to comment.