-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor: 로그인, 닉네임 관련 재사용 가능하게 분리 완료
- Loading branch information
1 parent
ee63c91
commit 5b9f6c4
Showing
4 changed files
with
81 additions
and
97 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useEffect } from 'react'; | ||
import { fetchUserAttributes } from 'aws-amplify/auth'; | ||
|
||
interface Props { | ||
setUserNickname: React.Dispatch<React.SetStateAction<string | null>>; | ||
} | ||
|
||
const UserNickname: React.FC<Props> = ({ setUserNickname }) => { | ||
useEffect(() => { | ||
handleFetchUserAttributes(); | ||
}, []); | ||
|
||
async function handleFetchUserAttributes() { | ||
try { | ||
const userAttributes = await fetchUserAttributes(); | ||
console.log(userAttributes); | ||
setUserNickname(userAttributes.nickname || null); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default UserNickname; |
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,43 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { getCurrentUser, signOut } from 'aws-amplify/auth'; | ||
|
||
export const useAuth = () => { | ||
const navigate = useNavigate(); | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
|
||
useEffect(() => { | ||
checkLoginStatus(); | ||
}, []); | ||
|
||
// 로그인된 상태인지 | ||
const checkLoginStatus = async () => { | ||
try { | ||
await getCurrentUser(); | ||
setIsLoggedIn(true); | ||
} catch (error) { | ||
setIsLoggedIn(false); | ||
} | ||
}; | ||
|
||
// 로그인 된 상태면 로그아웃 가능하게 함 | ||
const handleLoginOrLogout = () => { | ||
if (isLoggedIn) { | ||
handleSignOut(); | ||
} else { | ||
navigate('/login'); | ||
} | ||
}; | ||
|
||
const handleSignOut = async () => { | ||
try { | ||
await signOut({ global: true }); | ||
setIsLoggedIn(false); | ||
navigate('/'); | ||
} catch (error) { | ||
console.log('로그아웃 에러', error); | ||
} | ||
}; | ||
|
||
return { isLoggedIn, handleLoginOrLogout }; | ||
}; |