Skip to content

Commit 74889a1

Browse files
authored
Merge pull request #363 from Podo-Store/feat/enable-to-buy-script-for-admin
feat: 어드민 계정 구매 옵션 선택 가능하도록 추가
2 parents 69d387b + 5ff8694 commit 74889a1

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/contexts/AuthContext.jsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { useLocation } from "react-router-dom";
66
import { setupAxiosInterceptors } from "../utils/AxiosInterceptors";
77

88
import { SERVER_URL } from "../constants/ServerURL.js";
9-
import { ACCESS_TOKEN_EXP_TIME, REFRESH_TOKEN_EXP_TIME } from "../constants/TokenExpireTime";
9+
import {
10+
ACCESS_TOKEN_EXP_TIME,
11+
REFRESH_TOKEN_EXP_TIME,
12+
} from "../constants/TokenExpireTime";
1013

1114
const AuthContext = createContext();
1215

@@ -16,13 +19,35 @@ export const AuthProvider = ({ children }) => {
1619
// 페이지 로드 시 로컬 스토리지에서 유저 닉네임을 불러옴
1720
return localStorage.getItem("userNickname") || "username";
1821
});
22+
const [isAdmin, setIsAdmin] = useState(false);
1923

2024
const location = useLocation();
2125

26+
const parseIsAdmin = (token) => {
27+
if (!token) {
28+
return false;
29+
}
30+
try {
31+
const base64Url = token.split(".")[1];
32+
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
33+
const jsonPayload = decodeURIComponent(
34+
atob(base64)
35+
.split("")
36+
.map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2))
37+
.join("")
38+
);
39+
const payload = JSON.parse(jsonPayload);
40+
return payload?.auth ?? false;
41+
} catch (e) {
42+
return false;
43+
}
44+
};
45+
2246
useEffect(() => {
2347
const accessToken = Cookies.get("accessToken");
2448
const refreshToken = Cookies.get("refreshToken");
2549
setIsAuthenticated(!!accessToken && !!refreshToken);
50+
setIsAdmin(parseIsAdmin(accessToken));
2651
}, [location]);
2752

2853
const login = (accessToken, refreshToken, userNickname) => {
@@ -41,6 +66,7 @@ export const AuthProvider = ({ children }) => {
4166
localStorage.setItem("userNickname", userNickname);
4267

4368
setIsAuthenticated(true);
69+
setIsAdmin(parseIsAdmin(accessToken));
4470
};
4571

4672
const logout = () => {
@@ -51,6 +77,7 @@ export const AuthProvider = ({ children }) => {
5177
localStorage.removeItem("userNickname");
5278

5379
setIsAuthenticated(false);
80+
setIsAdmin(false);
5481
};
5582

5683
const refreshAccessToken = async () => {
@@ -77,6 +104,7 @@ export const AuthProvider = ({ children }) => {
77104
sameSite: "Strict",
78105
});
79106
setIsAuthenticated(true);
107+
setIsAdmin(parseIsAdmin(accessToken));
80108
return accessToken;
81109
} catch (error) {
82110
console.error("Access token refresh failed:", error);
@@ -98,6 +126,7 @@ export const AuthProvider = ({ children }) => {
98126
<AuthContext.Provider
99127
value={{
100128
isAuthenticated,
129+
isAdmin,
101130
userNickname,
102131
login,
103132
logout,

src/pages/work/Detail.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const Detail = () => {
101101
// 스크롤 시 bottom-bar visibility 변경
102102
const [isDetailBtnVisible, setIsDetailBtnVisible] = useState(false);
103103
const detailBtnWrapRef = useRef(null);
104-
const { isAuthenticated } = useContext(AuthContext);
104+
const { isAuthenticated, isAdmin } = useContext(AuthContext);
105105
const toggleLike = useSingleToggleLike();
106106
const [numPages, setNumPages] = useState<number | null>(null); // 페이지 수를 저장하는 상태 추가
107107

@@ -585,25 +585,25 @@ const Detail = () => {
585585

586586
<div className="option-select ">
587587
<Select
588-
className={`${script?.performance ? "cursor-pointer" : ""}`} // 포도알 스테이지에서만 적용
588+
className={isAdmin ? "cursor-pointer" : ""}
589589
value={selectedOption}
590590
onChange={onChangeSelectOption}
591-
disabled={!script?.performance} // 포도알 스테이지에서만 적용
591+
disabled={!isAdmin}
592592
>
593593
<option value="">옵션 선택</option>
594-
{/* {(script?.buyStatus === 0 || script?.buyStatus === 2) &&
595-
script.script ? (
594+
{(script?.buyStatus === 0 || script?.buyStatus === 2) &&
595+
script?.script ? (
596596
<option value="script">대본</option>
597597
) : null}
598598
{(script?.buyStatus === 0 || script?.buyStatus === 2) &&
599-
script.script &&
600-
script.performance ? (
599+
script?.script &&
600+
script?.performance ? (
601601
<option value="scriptPerform">대본 + 공연권</option>
602602
) : null}
603603
{(script?.buyStatus === 1 || script?.buyStatus === 2) &&
604-
script.performance ? (
604+
script?.performance ? (
605605
<option value="perform">공연권</option>
606-
) : null} */}
606+
) : null}
607607
</Select>
608608
</div>
609609

0 commit comments

Comments
 (0)