Skip to content

Commit

Permalink
feat: use is_username_taken db function
Browse files Browse the repository at this point in the history
Signed-off-by: Raphael Arce <raphael.arce@ts.berlin>
  • Loading branch information
raphael-arce committed Oct 14, 2024
1 parent 5a63c0e commit 3ef3b9e
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/components/profile/validation/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PasswordErrors, UsernameErrors } from "./types";
import { supabaseClient } from "../../../auth/supabase-client";
import { PostgrestSingleResponse } from "@supabase/supabase-js";

export function validatePassword(password: string): PasswordErrors {
const validLength = password.length > 7;
Expand Down Expand Up @@ -36,26 +37,19 @@ export async function checkIfUsernameIsTaken(
promise = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
supabaseClient
.from("profiles")
.select("username")
.eq("username", username)
.then(({ data, error }) => {
.rpc("is_username_taken", { given_username: username })
.then(({ data, error }: PostgrestSingleResponse<boolean>) => {
if (error) {
reject(error.message);
return;
}

if (!data) {
if (data === null || data === undefined) {
reject("could not check username");
return;
}

if (data.length > 0) {
resolve(true);
return;
}

resolve(false);
resolve(data);
});
}, 500);
});
Expand Down

0 comments on commit 3ef3b9e

Please sign in to comment.