-
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.
feat: Add login handler + auto login handler
- Loading branch information
Showing
8 changed files
with
155 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { AuthData, AuthResponse } from 'Auth'; | ||
import axios, { AxiosResponse } from 'axios'; | ||
|
||
class AuthApi { | ||
/** 로그인 */ | ||
login = async (loginId: string, password: string): Promise<AuthData> => { | ||
const data: AxiosResponse<AuthResponse> = await axios({ | ||
method: 'post', | ||
url: `${process.env.NEXT_PUBLIC_BASE_URL}/login`, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
data: { | ||
loginId, | ||
password, | ||
}, | ||
}); | ||
|
||
return data.data.data; | ||
}; | ||
} | ||
|
||
export default AuthApi; |
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,58 +1,8 @@ | ||
import { AuthResponse } from 'Auth'; | ||
import { atom } from 'jotai'; | ||
|
||
export const authInfoState = atom<AuthResponse | null>(null); | ||
|
||
export const dummyAuthData: AuthResponse = { | ||
success: true, | ||
code: 'successCode_xyz123', | ||
message: '로그인되었습니다.', | ||
data: { | ||
availableHomepages: [42, 69], | ||
isPrivacyPolicyAgree: true, | ||
privacyPolicyAgreePeriod: 3, | ||
dept: { | ||
id: 456, | ||
code: 'deptCode_abc789', | ||
name: '과학학부', | ||
}, | ||
accessToken: 'xyzabc789ghi', | ||
parentDept: { | ||
id: 457, | ||
code: 'parentDeptCode_def456', | ||
name: '자연대학', | ||
}, | ||
branch: { | ||
id: 123, | ||
name: '서부도서관', | ||
alias: '서부', | ||
libraryCode: 'libCode_ghi123', | ||
sortOrder: 2, | ||
}, | ||
showMobileMain: false, | ||
memberNo: 'member_xyz789', | ||
alternativeId: 'altid_ghi123', | ||
lastUpdated: '2023-07-27 15:23:45', | ||
branchGroup: { | ||
id: 24, | ||
name: '서울대학교', | ||
}, | ||
isPortalLogin: false, | ||
patronType: { | ||
id: 34, | ||
name: '교수', | ||
}, | ||
disableServices: ['ABC', 'DEF', 'GHI'], | ||
hasFamily: false, | ||
name: '홍길동', | ||
printMemberNo: 'print_xyz456', | ||
patronState: { | ||
id: 789, | ||
name: '퇴직', | ||
}, | ||
id: 987, | ||
multiTypePatrons: [], | ||
isExpired: false, | ||
isFamilyLogin: false, | ||
}, | ||
export type authInfoType = { | ||
name: string; | ||
sId: string; | ||
}; | ||
|
||
export const authInfoState = atom<authInfoType | null>(null); |
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,3 @@ | ||
export const QUERY_KEYS = { | ||
login: 'LOGIN', | ||
}; |
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,2 +1,4 @@ | ||
export { default as useVh } from './useVh'; | ||
export { default as useHeader } from './useHeader'; | ||
export { default as useInput } from './useInput'; | ||
export { default as useAuth } from './useAuth'; |
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,20 +1,62 @@ | ||
import AuthApi from '@/apis/auth'; | ||
import { authInfoState } from '@/atoms/authInfoState'; | ||
import { getUserInfo, updateUserInfo } from '@/utils/lib/infoHandler'; | ||
import { updateAccessToken } from '@/utils/lib/tokenHandler'; | ||
import { useAtom } from 'jotai'; | ||
import { useRouter } from 'next/router'; | ||
|
||
const useAuth = () => { | ||
const authApi = new AuthApi(); | ||
|
||
const router = useRouter(); | ||
const [authInfo, setAuthInfo] = useAtom(authInfoState); | ||
|
||
const handleLogin = () => { | ||
router.replace('/'); | ||
/** | ||
* 로그인 함수 | ||
*/ | ||
const handleLogin = async (id: string, password: string) => { | ||
try { | ||
const data = await authApi.login(id, password); | ||
console.log(data); | ||
setAuthInfo({ | ||
name: data.name, | ||
sId: data.printMemberNo, | ||
}); | ||
updateAccessToken(data.accessToken); | ||
updateUserInfo(data.name, data.printMemberNo, id, password); | ||
router.replace('/'); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
const checkAuth = () => { | ||
if (!authInfo) router.replace('/landing'); | ||
/** | ||
* 자동 로그인 함수 | ||
*/ | ||
const autoLogin = async () => { | ||
try { | ||
const userInfo = getUserInfo(); | ||
if (!userInfo) throw new Error('Empty user info'); | ||
const data = await authApi.login(userInfo.loginId, userInfo.password); | ||
console.log(data); | ||
setAuthInfo({ | ||
name: data.name, | ||
sId: data.printMemberNo, | ||
}); | ||
updateAccessToken(data.accessToken); | ||
updateUserInfo( | ||
data.name, | ||
data.printMemberNo, | ||
userInfo.loginId, | ||
userInfo.password, | ||
); | ||
} catch (err) { | ||
console.log(err); | ||
router.replace('/landing'); | ||
} | ||
}; | ||
|
||
return { authInfo, checkAuth, handleLogin }; | ||
return { authInfo, autoLogin, handleLogin }; | ||
}; | ||
|
||
export default useAuth; |
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,39 @@ | ||
export type StudentInfo = { | ||
name: string; | ||
sId: string; | ||
loginId: string; | ||
password: string; | ||
}; | ||
|
||
/** | ||
* 정보 가져오기 | ||
*/ | ||
export const getUserInfo = (): StudentInfo | null => { | ||
const data = localStorage.getItem('USER_INFO'); | ||
return data ? JSON.parse(data) : null; | ||
}; | ||
|
||
/** | ||
* 정보 업데이트 | ||
*/ | ||
export const updateUserInfo = ( | ||
name: string, | ||
sId: string, | ||
loginId: string, | ||
password: string, | ||
) => { | ||
const data = { | ||
name, | ||
sId, | ||
loginId, | ||
password, | ||
}; | ||
localStorage.setItem('USER_INFO', JSON.stringify(data)); | ||
}; | ||
|
||
/** | ||
* 정보 삭제 (로그아웃) | ||
*/ | ||
export const removeUserInfo = () => { | ||
localStorage.removeItem('USER_INFO'); | ||
}; |