mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 15:49:09 +03:00
* Remove usage of userManagementV2 flag and deprecate old user management UI component * Remove usage of API users endpoint, to use the same SQL query as users-infinite-query * Use user-infinite-query in UserImpersonationSelector and deprecate users-query * Fix import statements
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { useFlag } from 'hooks/ui/useFlag'
|
|
import { put } from 'lib/common/fetch'
|
|
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,
|
|
}
|
|
)
|
|
}
|