40 lines
883 B
TypeScript
40 lines
883 B
TypeScript
"use client";
|
|
|
|
import { useCallback } from "react";
|
|
import { useAtom } from "jotai";
|
|
import { authStateAtom, type AuthState } from "@/store/jotai/auth";
|
|
import type { LoginUser } from "@/types/auth";
|
|
|
|
export const useAuth = () => {
|
|
const [auth, setAuth] = useAtom(authStateAtom);
|
|
|
|
// 로그인 성공 후 LoginResponse 를 바로 넣어줄 수 있게
|
|
const setFromLoginResponse = useCallback(
|
|
(res: LoginUser, loginSeatNumber:string|null = null) => {
|
|
setAuth({
|
|
user: res
|
|
});
|
|
},
|
|
[setAuth],
|
|
);
|
|
|
|
const updateAuth = useCallback(
|
|
(next: AuthState | ((prev: AuthState) => AuthState)) => {
|
|
setAuth(next);
|
|
},
|
|
[setAuth],
|
|
);
|
|
|
|
const logout = useCallback(() => {
|
|
setAuth({
|
|
user: null,
|
|
});
|
|
}, [setAuth]);
|
|
|
|
return {
|
|
auth,
|
|
setAuth: updateAuth,
|
|
setFromLoginResponse,
|
|
logout,
|
|
};
|
|
} |