본문 바로가기

TIL

TIL 23.09.10 닉네임 중복검사

오늘은 프로필을 수정할 시, 닉네임 중복검사하는 로직을 추가했다.

 

여기서 neq 를 넣은 이유는, 유저가 정보를 수정할 때 중복검사를 해야하지만 현재 자신이 쓰고있는 닉네임에 대해서는 중복검사를 하지 않아야 하기 때문임 ! 그래서 다음과 같이 작성해주었다.

 

// 유저 프로필 닉네임 조회
export const fetchNicknameData = async (
  nickname: string,
  userId: string,
): Promise<ProfileType> => {
  try {
    const { data: nicknameData } = await supabase
      .from("user")
      .select("*")
      .neq("uid", userId)
      .eq("nickname", nickname);

    // 데이터가 없는 경우 null 반환
    if (!nicknameData) {
      return null;
    }

    // 데이터가 있는 경우 해당 데이터를 반환
    return nicknameData[0];
  } catch (error) {
    throw error;
  }
};

 

따라서 해당 닉네임이 존재할 경우 중복된 닉네임인것을 확인할 때, 중복된 닉네임이면 alert 가 뜨게 된다.

이 부분도 toastify 로 수정해야한다. 

 

const isNicknameValid = await fetchNicknameData(nickname, userId as string);

    if (isNicknameValid) {
      console.log(nickname);
      alert("중복된 닉네임 입니다. 다른 닉네임을 입력해주세요.");
      return;
    }