-
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.
Merge pull request #109 from Dev-FE-1/feat/is-valid-token-100
[Feature] 토큰 유효성 검사
- Loading branch information
Showing
9 changed files
with
128 additions
and
158 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 was deleted.
Oops, something went wrong.
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,54 +1,78 @@ | ||
import ApiClient from './ApiClient.js'; | ||
const login = async (email, password, showError) => { | ||
try { | ||
const response = await fetch('/api/login', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
|
||
export default class AuthService { | ||
constructor() { | ||
this.apiClient = new ApiClient(this.getToken); | ||
} | ||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
if (response.status === 400) { | ||
showError(`잘못된 요청입니다. ${errorData.error}`); | ||
} else if (response.status === 401) { | ||
showError( | ||
'로그인에 실패하였습니다. 이메일과 비밀번호를 다시 확인해 주시기 바랍니다', | ||
); | ||
} else { | ||
showError( | ||
`오류: ${errorData.message || '알 수 없는 오류가 발생했습니다.'}`, | ||
); | ||
} | ||
return; | ||
} | ||
|
||
login(email, password, showError) { | ||
return this.apiClient | ||
.post('/api/login', { | ||
email, | ||
password, | ||
}) | ||
.then((response) => { | ||
if (response.data.status === 'OK') { | ||
localStorage.setItem('token', response.data.token); | ||
window.location.href = '/'; | ||
} else { | ||
showError(response.data.error); | ||
} | ||
}) | ||
.catch((error) => { | ||
if (error.response && error.response.status) { | ||
if (error.response.status === 400) { | ||
showError(`잘못된 요청입니다. ${error.response.data.error}`); | ||
} else if (error.response.status === 401) { | ||
showError( | ||
'로그인에 실패하였습니다. 이메일과 비밀번호를 다시 확인해 주시기 바랍니다', | ||
); | ||
} else { | ||
showError(`오류: ${error.message}`); | ||
} | ||
} else { | ||
showError('알 수 없는 오류가 발생했습니다.'); | ||
} | ||
}); | ||
} | ||
const data = await response.json(); | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
logout() { | ||
localStorage.removeItem('token'); | ||
window.location.href = '/signin'; | ||
if (data.status === 'OK') { | ||
localStorage.setItem('token', data.token); | ||
window.location.href = '/'; | ||
} else { | ||
showError( | ||
`로그인에 실패하였습니다: ${data.message || '알 수 없는 오류가 발생했습니다.'}`, | ||
); | ||
} | ||
} catch (error) { | ||
showError('알 수 없는 오류가 발생했습니다.'); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
isLoggedIn() { | ||
return localStorage.getItem('token') !== null; | ||
const logout = () => { | ||
localStorage.removeItem('token'); | ||
window.location.href = '/signin'; | ||
}; | ||
|
||
const isLoggedIn = async () => { | ||
const token = localStorage.getItem('token'); | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getToken() { | ||
return localStorage.getItem('token'); | ||
try { | ||
const response = await fetch('/api/verify-token', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Token is not valid'); | ||
} | ||
|
||
const data = await response.json(); | ||
return data.valid; | ||
} catch (error) { | ||
console.error('Token verification failed:', error); | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
const getToken = () => { | ||
return localStorage.getItem('token'); | ||
}; | ||
|
||
export { login, logout, isLoggedIn, getToken }; |
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,21 +1,19 @@ | ||
import ApiClient from './ApiClient.js'; | ||
const loadPage = async (page, max) => { | ||
try { | ||
const response = await fetch(`/api/members/${page}?max=${max}`, { | ||
method: 'GET', | ||
}); | ||
|
||
export default class MemberService { | ||
constructor() { | ||
this.apiClient = new ApiClient(this.getToken); | ||
} | ||
if (!response.ok) { | ||
throw new Error(`Failed to load data: ${response.statusText}`); | ||
} | ||
|
||
loadPage(page, max) { | ||
return this.apiClient | ||
.get(`/api/members/${page}?max=${max}`) | ||
.then((response) => response.data.data) | ||
.catch((error) => { | ||
throw error; | ||
}); | ||
const data = await response.json(); | ||
return data.data; | ||
} catch (error) { | ||
console.error('Error loading page:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getToken() { | ||
return localStorage.getItem('token'); | ||
} | ||
} | ||
export { loadPage }; |
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
Oops, something went wrong.