재사용성을 위해 커스텀 훅으로 정리
까까오대화인 이유는 컨셉이 레트로..

코드
"use client";
import React from "react";
import { createClient } from "@/supabase/client";
import { useRouter } from "next/navigation";
import useAuthStore from "@/zustand/authStore";
const useKakao = () => {
const supabase = createClient();
const router = useRouter();
const { clearAuth } = useAuthStore();
// 로그인
const signInWithKakao = async () => {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: "kakao",
options: {
queryParams: { access_type: "offline", prompt: "select_account" },
redirectTo: `http://localhost:3000/api/auth/callback/`
}
});
};
// 로그아웃
const signOut = async () => {
const { error } = await supabase.auth.signOut();
clearAuth();
alert("로그아웃 되었습니다.");
router.push("/login");
};
return { signInWithKakao, signOut };
};
export default useKakao;

로그인 전역 상태 관리를 위해 도입!
처음에는 공식문서를 바탕으로 코드를 작성했다가 확장성이 있는 형태로 변경
(로그인 상태뿐만 아니라 유저 정보, 토큰등을 저장 해도 사용 가능하도록)
import { create } from "zustand";
type UserInfo = {
id: string;
email: string;
};
type State = {
isAuthenticated: boolean;
userInfo: UserInfo | null;
// token: string | null;
};
type Actions = {
// setAuth: (token: string, user: User) => void;
setAuth: (userInfo: UserInfo) => void;
clearAuth: () => void;
setIsAuthenticated: (status: boolean) => void;
};
const initialState: State = {
isAuthenticated: false,
userInfo: null
// token: null
};
const useAuthStore = create<State & Actions>((set) => ({
...initialState,
// setAuth: (token, user) => set({ isAuthenticated: true, token, user }),
// clearAuth: () => set({ isAuthenticated: false, token: null, user: null }),
setAuth: (userInfo) => set({ userInfo }),
clearAuth: () => set({ isAuthenticated: false, userInfo: null }),
setIsAuthenticated: (status) => set({ isAuthenticated: status })
}));
export default useAuthStore;
카카오 로그인 기능을 구현하는데 있어서 공식문서에서 말한대로 routehandler로 리다이렉트를 했는데 계속 오류가 발생
서로 다른 클라이언트 생성 사용
기존
클라이언트 사이드 애플리케이션(브라우저)에서 사용
변경
소셜 로그인 기능이 구현될지 몰랐지만 성공은 했다. 공식문서는 위대하다
zustand는 확실히 프로젝트 세팅 등 편리하고
redux보다 사용하기 쉽다.
생소해서 겁을 먹었을 뿐..!
오늘도 무엇인가 배워가긴 하는데 그것을 나중에도 써먹을 수 있도록 정리를 잘 해야하는데...
내 velog 임시저장에는 아직 정리들이 글들이 산더미
사실 notion에는 더 많음
