The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Convert direct requests to RQ in InviteUserModal and UserDropdown
Joshen Lim2508122a283a
diff --git a/studio/components/interfaces/Auth/Users/InviteUserModal.tsx b/studio/components/interfaces/Auth/Users/InviteUserModal.tsxindex f64c1723ccb..494c4b74516 100644--- a/studio/components/interfaces/Auth/Users/InviteUserModal.tsx+++ b/studio/components/interfaces/Auth/Users/InviteUserModal.tsx@@ -3,9 +3,9 @@ import { observer } from 'mobx-react-lite'import { useContext } from 'react'import { Button, Form, IconMail, Input, Modal } from 'ui'+import { useParams } from 'common'+import { useUserInviteMutation } from 'data/auth/user-invite-mutation'import { useCheckPermissions, useStore } from 'hooks'-import { post } from 'lib/common/fetch'-import { API_URL } from 'lib/constants'import { PageContext } from 'pages/project/[ref]/auth/users'export type InviteUserModalProps = {@@ -15,9 +15,11 @@ export type InviteUserModalProps = {const InviteUserModal = ({ visible, setVisible }: InviteUserModalProps) => {const { ui } = useStore()+ const { ref } = useParams()const PageState: any = useContext(PageContext)const handleToggle = () => setVisible(!visible)+ const { mutateAsync: inviteUser, isLoading: isInviting } = useUserInviteMutation()const canInviteUsers = useCheckPermissions(PermissionAction.AUTH_EXECUTE, 'invite_user')const validate = (values: any) => {@@ -34,27 +36,13 @@ const InviteUserModal = ({ visible, setVisible }: InviteUserModalProps) => {return errors}- const onInviteUser = async (values: any, { setSubmitting }: any) => {- setSubmitting(true)+ const onInviteUser = async (values: any) => {+ if (!ref) return console.error('Project ref is required')- const response = await post(`${API_URL}/auth/${PageState.projectRef}/invite`, {- email: values.email,- })- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to invite user: ${response.error.message}`,- })- } else {- PageState.fetchData(1)- ui.setNotification({- category: 'success',- message: `Sent invite email to ${values.email}`,- })- setVisible(false)- }-- setSubmitting(false)+ await inviteUser({ projectRef: ref, email: values.email })+ PageState.fetchData(1)+ ui.setNotification({ category: 'success', message: `Sent invite email to ${values.email}` })+ setVisible(false)}return (@@ -74,7 +62,7 @@ const InviteUserModal = ({ visible, setVisible }: InviteUserModalProps) => {validate={validate}onSubmit={onInviteUser}>- {({ isSubmitting }: { isSubmitting: boolean }) => (+ {() => (<div className="space-y-6 py-4"><Modal.Content><Input@@ -93,8 +81,8 @@ const InviteUserModal = ({ visible, setVisible }: InviteUserModalProps) => {blocksize="small"htmlType="submit"- loading={isSubmitting}- disabled={!canInviteUsers || isSubmitting}+ loading={isInviting}+ disabled={!canInviteUsers || isInviting}>Invite user</Button>diff --git a/studio/components/interfaces/Auth/Users/UserDropdown.tsx b/studio/components/interfaces/Auth/Users/UserDropdown.tsxindex 67c06b4da33..0f718cd44a9 100644--- a/studio/components/interfaces/Auth/Users/UserDropdown.tsx+++ b/studio/components/interfaces/Auth/Users/UserDropdown.tsx@@ -1,149 +1,93 @@-import { FC, useContext, useState } from 'react'-import { observer } from 'mobx-react-lite'import * as Tooltip from '@radix-ui/react-tooltip'-import { Button, Dropdown, IconTrash, IconMail, IconMoreHorizontal, IconShieldOff } from 'ui'+import { observer } from 'mobx-react-lite'+import { useContext } from 'react'+import { Button, Dropdown, IconMail, IconMoreHorizontal, IconShieldOff, IconTrash } from 'ui'+import { useParams } from 'common'+import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmModal'+import { useUserDeleteMFAFactorsMutation } from 'data/auth/user-delete-mfa-factors-mutation'+import { useUserDeleteMutation } from 'data/auth/user-delete-mutation'+import { useUserResetPasswordMutation } from 'data/auth/user-reset-password-mutation'+import { useUserSendMagicLinkMutation } from 'data/auth/user-send-magic-link-mutation'+import { useUserSendOTPMutation } from 'data/auth/user-send-otp-mutation'import { useStore } from 'hooks'import { timeout } from 'lib/helpers'-import { post, delete_ } from 'lib/common/fetch'-import { API_URL } from 'lib/constants'import { PageContext } from 'pages/project/[ref]/auth/users'-import { confirmAlert } from 'components/to-be-cleaned/ModalsDeprecated/ConfirmModal'import { User } from './Users.types'-interface Props {+interface UserDropdownProps {user: UsercanRemoveUser: booleancanRemoveMFAFactors: boolean}-const UserDropdown: FC<Props> = ({ user, canRemoveUser, canRemoveMFAFactors }) => {- const PageState: any = useContext(PageContext)+const UserDropdown = ({ user, canRemoveUser, canRemoveMFAFactors }: UserDropdownProps) => {const { ui } = useStore()- const [loading, setLoading] = useState<boolean>(false)+ const { ref } = useParams()+ const PageState: any = useContext(PageContext)- async function handleResetPassword() {- try {- setLoading(true)- const response = await post(`${API_URL}/auth/${PageState.projectRef}/recover`, user)- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to send password recovery: ${response.error.message}`,- })- } else {- ui.setNotification({- category: 'success',- message: `Sent password recovery to ${user.email}`,- })- }- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Send password recovery failed: ${error?.message}`,- })- } finally {- setLoading(false)- }+ const { mutateAsync: resetPassword, isLoading: isResetting } = useUserResetPasswordMutation()+ const { mutateAsync: sendMagicLink, isLoading: isSendingLink } = useUserSendMagicLinkMutation()+ const { mutateAsync: sendOTP, isLoading: isSendingOTP } = useUserSendOTPMutation()+ const { mutateAsync: deleteUser, isLoading: isDeleting } = useUserDeleteMutation()+ const { mutateAsync: deleteUserMFAFactors, isLoading: isDeletingFactors } =+ useUserDeleteMFAFactorsMutation()+ const isLoading = isResetting || isSendingLink || isSendingOTP || isDeleting || isDeletingFactors++ const handleResetPassword = async () => {+ if (!ref) return console.error('Project ref is required')+ await resetPassword({ projectRef: ref, user })+ ui.setNotification({+ category: 'success',+ message: `Sent password recovery to ${user.email}`,+ })}async function handleSendMagicLink() {- try {- setLoading(true)- const response = await post(`${API_URL}/auth/${PageState.projectRef}/magiclink`, user)- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to send magic link: ${response.error.message}`,- })- } else {- ui.setNotification({- category: 'success',- message: `Sent magic link to ${user.email}`,- })- }- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to send magic link: ${error?.message}`,- })- } finally {- setLoading(false)- }+ if (!ref) return console.error('Project ref is required')+ await sendMagicLink({ projectRef: ref, user })+ ui.setNotification({+ category: 'success',+ message: `Sent magic link to ${user.email}`,+ })}async function handleSendOtp() {- try {- setLoading(true)- const response = await post(`${API_URL}/auth/${PageState.projectRef}/otp`, user)- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to OTP: ${response.error.message}`,- })- } else {- ui.setNotification({- category: 'success',- message: `Sent OTP to ${user.phone}`,- })- }- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to send OTP: ${error?.message}`,- })- } finally {- setLoading(false)- }+ if (!ref) return console.error('Project ref is required')+ await sendOTP({ projectRef: ref, user })+ ui.setNotification({+ category: 'success',+ message: `Sent OTP to ${user.phone}`,+ })}async function handleDelete() {await timeout(200)-confirmAlert({title: 'Confirm to delete',message: `This is permanent! Are you sure you want to delete user ${user.email} ?`,onAsyncConfirm: async () => {- setLoading(true)- const response = await delete_(`${API_URL}/auth/${PageState.projectRef}/users`, user)- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to delete user: ${response.error.message}`,- })- } else {- ui.setNotification({ category: 'success', message: `Successfully deleted ${user.email}` })- PageState.users = PageState.users.filter((x: any) => x.id != user.id)- PageState.totalUsers -= 1- }- setLoading(false)+ if (!ref) return console.error('Project ref is required')+ await deleteUser({ projectRef: ref, user })+ ui.setNotification({ category: 'success', message: `Successfully deleted ${user.email}` })+ PageState.users = PageState.users.filter((x: any) => x.id != user.id)+ PageState.totalUsers -= 1},})}async function handleDeleteFactors() {await timeout(200)-confirmAlert({title: 'Confirm to delete',message: `This is permanent! Are you sure you want to delete the user's MFA factors?`,onAsyncConfirm: async () => {- setLoading(true)- const response = await delete_(- `${API_URL}/auth/${PageState.projectRef}/users/${user.id}/factors`- )- if (response.error) {- ui.setNotification({- category: 'error',- message: `Failed to delete factors: ${response.error.message}`,- })- } else {- ui.setNotification({- category: 'success',- message: "Successfully deleted the user's factors",- })- }- setLoading(false)+ if (!ref) return console.error('Project ref is required')+ await deleteUserMFAFactors({ projectRef: ref, userId: user.id })+ ui.setNotification({+ category: 'success',+ message: "Successfully deleted the user's factors",+ })},})}@@ -236,10 +180,10 @@ const UserDropdown: FC<Props> = ({ user, canRemoveUser, canRemoveMFAFactors }) =asChildtype="text"icon={<IconMoreHorizontal />}- loading={loading}+ loading={isLoading}className="hover:border-gray-500">- <span></span>+ <span /></Button></Dropdown>)diff --git a/studio/data/auth/user-delete-mfa-factors-mutation.ts b/studio/data/auth/user-delete-mfa-factors-mutation.tsnew file mode 100644index 00000000000..29c16bb969c--- /dev/null+++ b/studio/data/auth/user-delete-mfa-factors-mutation.ts@@ -0,0 +1,47 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { User } from 'components/interfaces/Auth/Users/Users.types'+import { delete_ } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { ResponseError } from 'types'++export type UserDeleteMFAFactorsVariables = {+ projectRef: string+ userId: string+}++export async function deleteMFAFactors({ projectRef, userId }: UserDeleteMFAFactorsVariables) {+ const response = await delete_(`${API_URL}/auth/${projectRef}/users/${userId}/factors`)+ if (response.error) throw response.error+ return response+}++type UserDeleteMFAFactorsData = Awaited<ReturnType<typeof deleteMFAFactors>>++export const useUserDeleteMFAFactorsMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserDeleteMFAFactorsData, ResponseError, UserDeleteMFAFactorsVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserDeleteMFAFactorsData, ResponseError, UserDeleteMFAFactorsVariables>(+ (vars) => deleteMFAFactors(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to delete user MFA factors: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/auth/user-delete-mutation.ts b/studio/data/auth/user-delete-mutation.tsnew file mode 100644index 00000000000..afae9bc01bb--- /dev/null+++ b/studio/data/auth/user-delete-mutation.ts@@ -0,0 +1,47 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { User } from 'components/interfaces/Auth/Users/Users.types'+import { delete_ } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { ResponseError } from 'types'++export type UserDeleteVariables = {+ projectRef: string+ user: User+}++export async function deleteUser({ projectRef, user }: UserDeleteVariables) {+ const response = await delete_(`${API_URL}/auth/${projectRef}/users`, user)+ if (response.error) throw response.error+ return response+}++type UserDeleteData = Awaited<ReturnType<typeof deleteUser>>++export const useUserDeleteMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserDeleteData, ResponseError, UserDeleteVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserDeleteData, ResponseError, UserDeleteVariables>(+ (vars) => deleteUser(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to delete user: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/auth/user-invite-mutation.ts b/studio/data/auth/user-invite-mutation.tsnew file mode 100644index 00000000000..8f30c6ea427--- /dev/null+++ b/studio/data/auth/user-invite-mutation.ts@@ -0,0 +1,46 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { post } from 'lib/common/fetch'+import { ResponseError } from 'types'+import { API_URL } from 'lib/constants'++export type UserInviteVariables = {+ projectRef: string+ email: string+}++export async function inviteUser({ projectRef, email }: UserInviteVariables) {+ const response = await post(`${API_URL}/auth/${projectRef}/invite`, { email })+ if (response.error) throw response.error+ return response+}++type UserInviteData = Awaited<ReturnType<typeof inviteUser>>++export const useUserInviteMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserInviteData, ResponseError, UserInviteVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserInviteData, ResponseError, UserInviteVariables>(+ (vars) => inviteUser(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to invite user: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/auth/user-reset-password-mutation.ts b/studio/data/auth/user-reset-password-mutation.tsnew file mode 100644index 00000000000..2c49e53e570--- /dev/null+++ b/studio/data/auth/user-reset-password-mutation.ts@@ -0,0 +1,47 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { User } from 'components/interfaces/Auth/Users/Users.types'+import { post } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { ResponseError } from 'types'++export type UserResetPasswordVariables = {+ projectRef: string+ user: User+}++export async function resetPassword({ projectRef, user }: UserResetPasswordVariables) {+ const response = await post(`${API_URL}/auth/${projectRef}/recover`, user)+ if (response.error) throw response.error+ return response+}++type UserResetPasswordData = Awaited<ReturnType<typeof resetPassword>>++export const useUserResetPasswordMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserResetPasswordData, ResponseError, UserResetPasswordVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserResetPasswordData, ResponseError, UserResetPasswordVariables>(+ (vars) => resetPassword(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to reset user password: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/auth/user-send-magic-link-mutation.ts b/studio/data/auth/user-send-magic-link-mutation.tsnew file mode 100644index 00000000000..936f0f49230--- /dev/null+++ b/studio/data/auth/user-send-magic-link-mutation.ts@@ -0,0 +1,47 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { User } from 'components/interfaces/Auth/Users/Users.types'+import { post } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { ResponseError } from 'types'++export type UserSendMagicLinkVariables = {+ projectRef: string+ user: User+}++export async function sendMagicLink({ projectRef, user }: UserSendMagicLinkVariables) {+ const response = await post(`${API_URL}/auth/${projectRef}/magiclink`, user)+ if (response.error) throw response.error+ return response+}++type UserSendMagicLinkData = Awaited<ReturnType<typeof sendMagicLink>>++export const useUserSendMagicLinkMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserSendMagicLinkData, ResponseError, UserSendMagicLinkVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserSendMagicLinkData, ResponseError, UserSendMagicLinkVariables>(+ (vars) => sendMagicLink(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to send magic link: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/studio/data/auth/user-send-otp-mutation.ts b/studio/data/auth/user-send-otp-mutation.tsnew file mode 100644index 00000000000..5265e9f40cd--- /dev/null+++ b/studio/data/auth/user-send-otp-mutation.ts@@ -0,0 +1,47 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'++import { User } from 'components/interfaces/Auth/Users/Users.types'+import { post } from 'lib/common/fetch'+import { API_URL } from 'lib/constants'+import { ResponseError } from 'types'++export type UserSendOTPVariables = {+ projectRef: string+ user: User+}++export async function sendOTP({ projectRef, user }: UserSendOTPVariables) {+ const response = await post(`${API_URL}/auth/${projectRef}/otp`, user)+ if (response.error) throw response.error+ return response+}++type UserSendOTPData = Awaited<ReturnType<typeof sendOTP>>++export const useUserSendOTPMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<UserSendOTPData, ResponseError, UserSendOTPVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<UserSendOTPData, ResponseError, UserSendOTPVariables>(+ (vars) => sendOTP(vars),+ {+ async onSuccess(data, variables, context) {+ // [Joshen] If we need to invalidate any queries+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to send magic link: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}