Skip to content

Commit

Permalink
Merge pull request #55 from potato-club/add/adminPage/checkSignUpPage
Browse files Browse the repository at this point in the history
[#47] Feat CheckSignUpPage
  • Loading branch information
JoGeumJu authored Jul 2, 2023
2 parents cf88a50 + 61b046b commit b59ab45
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/components/adminLoginPage/AdminLoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const AdminLoginPage = () => {
}) => {
localStorage.setItem("token", data.accessToken);
localStorage.setItem("refreshToken", data.refreshToken);
setUserInfo({ email: "", name: "관리자", picture: "", role: "admin" });
localStorage.setItem("role", "ADMIN");
handleGoPrevPath();
};
const failLogin = () => {
Expand All @@ -35,10 +35,11 @@ export const AdminLoginPage = () => {
const { mutate } = useQueryPostLogin(completeLogin, failLogin);

useEffect(() => {
if (userInfo.role === "USER") {
if (localStorage.getItem("role") === "USER") {
localStorage.removeItem("token");
localStorage.removeItem("refreshToken");
} else if (userInfo.role === "ADMIN") {
localStorage.removeItem("role");
} else if (localStorage.getItem("role") === "ADMIN") {
handleGoPrevPath();
}
}, []);
Expand All @@ -55,7 +56,9 @@ export const AdminLoginPage = () => {
};
const handleGoPrevPath = () => {
const prevPath = localStorage.getItem("prevPath");
prevPath !== null && isContainPathName(prevPath)
prevPath !== null &&
isContainPathName(prevPath) &&
prevPath.includes(pathName.CHECK_SIGNUP.LIST)
? Router.push(prevPath)
: Router.push(pathName.CHECK_SIGNUP.LIST);
};
Expand Down
14 changes: 6 additions & 8 deletions src/components/header/AdminHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Router from "next/router";
import { useEffect } from "react";
import { useRecoilValue } from "recoil";
import styled, { css } from "styled-components";
import { pathName } from "../../config/adminPathName";
import { userInformation } from "../../recoil/userInfo";
import { customColor } from "../customColor";
import { MdLogout } from "react-icons/md";

Expand All @@ -12,16 +10,18 @@ interface MenuProps {
}

export const AdminHeader = () => {
const userInfo = useRecoilValue(userInformation);
useEffect(() => {
if (localStorage.getItem("token") === null || userInfo.role === "USER") {
if (
localStorage.getItem("token") === null ||
localStorage.getItem("role") === "USER"
)
handleLogout();
}
}, []);

const handleLogout = () => {
localStorage.removeItem("token");
localStorage.removeItem("refreshToken");
localStorage.removeItem("role");
localStorage.setItem("prevPath", Router.asPath.replace(/\/\d+$/, ""));
Router.push(pathName.LOGIN);
};
Expand All @@ -46,9 +46,7 @@ export const AdminHeader = () => {
<MenuButton isPath={false}>직원정보수정</MenuButton>
<MenuButton isPath={false}>통계추출</MenuButton>
</WrapperInner>
{!(
localStorage.getItem("token") === null || userInfo.role === "USER"
) && (
{!(localStorage.getItem("token") === null) && (
<Logout onClick={handleLogout}>
로그아웃
<LogoutIcon />
Expand Down
14 changes: 10 additions & 4 deletions src/components/header/components/MyInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ export const MyInfo = () => {
const [userInfo, setUserInfo] = useRecoilState(userInformation);

useEffect(() => {
setIsLoginState(localStorage?.getItem("token") !== null);
if (localStorage?.getItem("token") !== null) {
getUserInfo();
if (!Router.asPath.includes("/admin")) {
setIsLoginState(localStorage?.getItem("token") !== null);
if (localStorage?.getItem("token") !== null) {
if (localStorage.getItem("role") === "ADMIN") {
handleLogout();
} else {
getUserInfo();
}
}
}
}, []);

Expand All @@ -35,7 +41,6 @@ export const MyInfo = () => {
email: data.data.email,
name: data.data.name,
picture: data.data.picture,
role: data.data.role,
});
})
.catch((error) => {
Expand All @@ -50,6 +55,7 @@ export const MyInfo = () => {
const handleLogout = () => {
localStorage.removeItem("token");
localStorage.removeItem("refreshToken");
localStorage.removeItem("role");
setIsLoginState(false);
Router.reload();
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Props {
export const Layout = ({ children }: Props) => {
const [isAdmin, setIsAdmin] = useState(false);
useEffect(() => {
setIsAdmin(Router.asPath.includes("/admin"));
setIsAdmin(Router.asPath.includes(pathName.CHECK_SIGNUP.LIST));
}, []);

return (
Expand Down
5 changes: 3 additions & 2 deletions src/components/loginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import styled from "styled-components";
import Image from "next/image";
import { customColor } from "../customColor";
import Link from "next/link";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import Router from "next/router";
import { pathName } from "../../config/pathName";

Expand All @@ -23,7 +23,7 @@ export const LoginPage = () => {
const handleGoPrevPath = () => {
const prevPath = localStorage.getItem("prevPath");
prevPath !== null &&
prevPath !== pathName.LOGIN &&
!prevPath.includes(pathName.LOGIN) &&
isContainPathName(prevPath)
? Router.push(prevPath)
: Router.push(pathName.MAIN);
Expand All @@ -42,6 +42,7 @@ export const LoginPage = () => {
if (token && refreshToken) {
localStorage.setItem("token", token);
localStorage.setItem("refreshToken", refreshToken);
localStorage.setItem("role", "USER");
handleGoPrevPath();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/recoil/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface UserInfo {
email: string;
name: string;
picture: string;
role: string;
}

export const isLogin = atom<boolean>({
Expand All @@ -14,5 +13,5 @@ export const isLogin = atom<boolean>({

export const userInformation = atom<UserInfo>({
key: "userInformation",
default: { email: "", name: "", picture: "", role: "" },
default: { email: "", name: "", picture: "" },
});

0 comments on commit b59ab45

Please sign in to comment.