-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from THEGOODs-repo/fixmerge
Fix : Login Token 관련
- Loading branch information
Showing
9 changed files
with
144 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,56 @@ | ||
import { useDispatch } from 'react-redux'; | ||
import { Link } from 'react-router-dom' | ||
import styled from 'styled-components' | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { Link } from "react-router-dom"; | ||
import styled from "styled-components"; | ||
import { setExpire, setToken } from "../../redux/loginSlice"; | ||
|
||
const StyledLink = styled(Link)` | ||
text-decoration: none; /* underline 제거 */ | ||
color: gray; /* 글씨 색상 설정 */ | ||
margin: 0 10px; /* 좌우 여백 추가 */ | ||
text-decoration: none; /* underline 제거 */ | ||
color: gray; /* 글씨 색상 설정 */ | ||
margin: 0 10px; /* 좌우 여백 추가 */ | ||
`; | ||
|
||
|
||
function HeaderComponent() { | ||
|
||
return ( | ||
<> | ||
<SubHeaderContainer> | ||
<SubHeaderItemContainer> | ||
{/* <img src="../img/" alt="bell" /> | ||
const dispatch = useDispatch(); | ||
const token = useSelector((state) => state.login.token); | ||
return ( | ||
<> | ||
<SubHeaderContainer> | ||
<SubHeaderItemContainer> | ||
{/* <img src="../img/" alt="bell" /> | ||
알림 */} | ||
<StyledLink to="/register">회원가입</StyledLink> | ||
<StyledLink to="/login">로그인</StyledLink> | ||
<StyledLink>비회원 주문조회</StyledLink> | ||
</SubHeaderItemContainer> | ||
</SubHeaderContainer> | ||
</> | ||
) | ||
|
||
<StyledLink to="/register">회원가입</StyledLink> | ||
{token == null ? ( | ||
<StyledLink to="/login">로그인</StyledLink> | ||
) : ( | ||
<StyledLink | ||
onClick={() => { | ||
dispatch(setToken(null)); | ||
dispatch(setExpire(null)); | ||
}} | ||
> | ||
로그아웃 | ||
</StyledLink> | ||
)} | ||
<StyledLink>비회원 주문조회</StyledLink> | ||
</SubHeaderItemContainer> | ||
</SubHeaderContainer> | ||
</> | ||
); | ||
} | ||
|
||
export default HeaderComponent | ||
export default HeaderComponent; | ||
|
||
const SubHeaderContainer = styled.div` | ||
width : 100%; | ||
height : 47px; | ||
background: rgba(156, 156, 156, 0.20); | ||
display : flex; | ||
justify-content : flex-end; | ||
align-items : center; | ||
` | ||
width: 100%; | ||
height: 47px; | ||
background: rgba(156, 156, 156, 0.2); | ||
display: flex; | ||
justify-content: flex-end; | ||
align-items: center; | ||
`; | ||
const SubHeaderItemContainer = styled.div` | ||
display : flex; | ||
width : 300px; | ||
height : 23px; | ||
margin-right : 310px; | ||
` | ||
display: flex; | ||
width: 300px; | ||
height: 23px; | ||
margin-right: 310px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useLocation } from "react-router-dom"; | ||
import { useEffect } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import axios from "axios"; | ||
import { setToken, setExpire } from "../../redux/loginSlice"; | ||
import Cookies from "js-cookie"; | ||
|
||
export default function TokenTrigger() { | ||
const dispatch = useDispatch(); | ||
const location = useLocation(); | ||
const token = useSelector((state) => state.login.token); | ||
const expire = useSelector((state) => state.login.expire); | ||
|
||
useEffect(() => { | ||
if (token != null) { | ||
console.log(Cookies); | ||
let now = new Date(); | ||
let expires = new Date(expire); | ||
if (expires < now) { | ||
fetchLogin(); | ||
} | ||
} | ||
}, [location]); | ||
|
||
const fetchLogin = async () => { | ||
try { | ||
const endpoint = `/api/members/token/regenerate`; | ||
const requestBody = { | ||
refreshToken: Cookies.get("refreshToken"), | ||
}; | ||
|
||
const response = await axios.post(endpoint, requestBody, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (response.data.isSuccess === true) { | ||
console.log(response); | ||
dispatch(setToken(response.data.result.accessToken)); | ||
if (!isNaN(Date.parse(response.data.result.accessExpireTime))) { | ||
let expires = new Date(response.data.result.accessExpireTime); | ||
dispatch(setExpire(expires)); | ||
} | ||
} | ||
} catch (error) { | ||
if (error.response) { | ||
if (error.response.status === 400) { | ||
} else if (error.response.status === 500) { | ||
} else { | ||
console.log("Unhandled error:", error.response.data); | ||
} | ||
} else if (error.request) { | ||
console.log("No response received:", error.request); | ||
} else { | ||
console.log("Error:", error.message); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters