Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Convert password check to RQ and update lib/helpers

Joshen Lim6e70fda77b78
diff --git a/studio/data/profile/password-check-mutation.ts b/studio/data/profile/password-check-mutation.ts
new file mode 100644
index 00000000000..32e6bc7acd6
--- /dev/null
+++ b/studio/data/profile/password-check-mutation.ts
@@ -0,0 +1,43 @@
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+
+export type PasswordCheckVariables = {
+ password: string
+}
+
+export async function checkPasswordStrength({ password }: PasswordCheckVariables) {
+ const { data, error } = await post('/platform/profile/password-check', { body: { password } })
+ if (error) throw error
+ return data
+}
+
+type PasswordCheckData = Awaited<ReturnType<typeof checkPasswordStrength>>
+
+export const usePasswordCheckMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<PasswordCheckData, ResponseError, PasswordCheckVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<PasswordCheckData, ResponseError, PasswordCheckVariables>(
+ (vars) => checkPasswordStrength(vars),
+ {
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to check password: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 71246f591b5..a03468b00de 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -1,3 +1,4 @@
+import { checkPasswordStrength } from 'data/profile/password-check-mutation'
import { post } from 'lib/common/fetch'
import { API_URL, DEFAULT_MINIMUM_PASSWORD_STRENGTH, PASSWORD_STRENGTH } from 'lib/constants'
import { toast } from 'react-hot-toast'
@@ -180,37 +181,35 @@ export const copyToClipboard = async (str: string | Promise<string>, callback =
}
export async function passwordStrength(value: string) {
- let message = ''
- let warning = ''
- let strength = 0
+ let message: string = ''
+ let warning: string = ''
+ let strength: number = 0
if (value && value !== '') {
if (value.length > 99) {
message = `${PASSWORD_STRENGTH[0]} Maximum length of password exceeded`
warning = `Password should be less than 100 characters`
} else {
- const response = await post(`${API_URL}/profile/password-check`, { password: value })
- if (!response.error) {
- const { result } = response
- const score = (PASSWORD_STRENGTH as any)[result.score]
+ try {
+ const { result } = await checkPasswordStrength({ password: value })
+ const resultScore = result?.score ?? 0
+
+ const score = (PASSWORD_STRENGTH as any)[resultScore]
const suggestions = result.feedback?.suggestions
? result.feedback.suggestions.join(' ')
: ''
- // set message :string
message = `${score} ${suggestions}`
-
- // set strength :number
- strength = result.score
+ strength = resultScore
// warning message for anything below 4 strength :string
- if (result.score < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
+ if (resultScore < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
warning = `${
result?.feedback?.warning ? result?.feedback?.warning + '.' : ''
} You need a stronger password.`
}
- } else {
- toast.error(`Failed to check password strength: ${response.error.message}`)
+ } catch (error: any) {
+ toast.error(`Failed to check password strength: ${error.message}`)
}
}
}