feat: 로그인 화면 및 공통 속성 생성
This commit is contained in:
31
src/store/auth.tsx
Normal file
31
src/store/auth.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import React, { createContext, useContext, useMemo, useState } from "react";
|
||||
|
||||
export type AuthContextValue = {
|
||||
isAuthed: boolean;
|
||||
login: () => void;
|
||||
logout: () => void;
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [isAuthed, setIsAuthed] = useState<boolean>(false);
|
||||
console.log("isAuthed: " + isAuthed);
|
||||
|
||||
const value = useMemo<AuthContextValue>(
|
||||
() => ({
|
||||
isAuthed,
|
||||
login: () => setIsAuthed(true),
|
||||
logout: () => setIsAuthed(false),
|
||||
}),
|
||||
[isAuthed]
|
||||
);
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) throw new Error("useAuth must be used within AuthProvider");
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user