260123_ENH : Layout가지 완성된 최초 commit
This commit is contained in:
42
src/hooks/useAuth.ts
Normal file
42
src/hooks/useAuth.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
"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,
|
||||
seatNumber: loginSeatNumber,
|
||||
});
|
||||
},
|
||||
[setAuth],
|
||||
);
|
||||
|
||||
const updateAuth = useCallback(
|
||||
(next: AuthState | ((prev: AuthState) => AuthState)) => {
|
||||
setAuth(next);
|
||||
},
|
||||
[setAuth],
|
||||
);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
setAuth({
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
});
|
||||
}, [setAuth]);
|
||||
|
||||
return {
|
||||
auth,
|
||||
setAuth: updateAuth,
|
||||
setFromLoginResponse,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user