mirror of
https://github.com/supabase/supabase.git
synced 2026-10-17 23:29:08 +03:00
* Fix the types for API settings to include protocol. * Use the protocol property in all usages of endpoints. * Update * Lint * Revert changes to retrieving protocol * Remove console.logs --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { put } from 'lib/common/fetch'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import { PROJECT_ENDPOINT_PROTOCOL } from 'pages/api/constants'
|
|
import type { ResponseError } from 'types'
|
|
import { authKeys } from './keys'
|
|
|
|
export type UserUpdateVariables = {
|
|
projectRef?: string
|
|
protocol: string
|
|
endpoint: string
|
|
serviceApiKey: string
|
|
|
|
userId: string
|
|
// For now just support updating banning the user
|
|
banDuration: number | 'none' // In hours, "none" to unban, otherwise a string in hours e.g "24h"
|
|
}
|
|
|
|
export async function updateUser({
|
|
protocol,
|
|
endpoint,
|
|
serviceApiKey,
|
|
userId,
|
|
banDuration,
|
|
}: UserUpdateVariables) {
|
|
// [Joshen] This is probably the only endpoint that needs the put method from lib/common/fetch
|
|
// as it's not our internal API.
|
|
const response = await put(
|
|
`${protocol}://${endpoint}/auth/v1/admin/users/${userId}`,
|
|
{ ban_duration: typeof banDuration === 'number' ? `${banDuration}h` : banDuration },
|
|
{
|
|
headers: {
|
|
apikey: serviceApiKey,
|
|
Authorization: `Bearer ${serviceApiKey}`,
|
|
},
|
|
credentials: undefined,
|
|
}
|
|
)
|
|
|
|
if (response.error) throw response.error
|
|
return response
|
|
}
|
|
|
|
type UserUpdateData = Awaited<ReturnType<typeof updateUser>>
|
|
|
|
export const useUserUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<UserUpdateData, ResponseError, UserUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<UserUpdateData, ResponseError, UserUpdateVariables>(
|
|
(vars) => updateUser(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(authKeys.usersInfinite(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update user: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|