백엔드/Supabase

[Supabase] 회원가입 기능 구현

순코딩 2024. 12. 19. 09:44
src / service / auth / signUp.ts
import { supabase } from "@/lib/supabaseClient";
import { signUpType } from "@/types/auth/signUp.type";

export async function signUp({ email, password }: signUpType) {
  const { data, error } = await supabase.auth.signUp({ email, password });
  return { data, error };
}

 

src / hooks / useAuth.ts

import { useModalStore } from "@/store";
import { useRouter } from "next/navigation";
import { signUpType } from "@/types/auth/signUp.type";

const useAuth = () => {
  const { open } = useModalStore();
  const router = useRouter();

  // 회원가입
  async function handleSignUp({ email, password }: signUpType) {
    const { data, error } = await signUp({ email, password });

    // 이메일 중복 검사
    if (data?.user?.aud === "authenticated") {
      open({
        title: "Error",
        content: `회원가입 에러 발생
        이미 가입된 이메일입니다.
        `,
      });
    }
    // 에러 검사
    else if (error) {
      open({
        title: "Error",
        content: `회원가입 에러 발생
        잠시 후 다시 시도해주세요.
        `,
      });
    }
    // 인증 메일 발송 완료 페이지로 리다이렉트
    else {
      router.push("/sign-up/email-sent");
    }
  }

  return { handleSignUp };
};

export default useAuth;