-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 사용자의 이메일, 비밀번호를 입력받고 토큰을 발급받는다. related to #31
- Loading branch information
1 parent
7a8bf37
commit 897be90
Showing
17 changed files
with
91 additions
and
112 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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,3 +1,13 @@ | ||
import axios from 'axios'; | ||
|
||
export default axios.create({ baseURL: 'https://api.doit-toeic.xyz' }); | ||
export default axios.create({ | ||
baseURL: 'https://api.doit-toeic.xyz', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
export const httpClientForCredentials = axios.create({ | ||
baseURL: 'https://api.doit-toeic.xyz', | ||
withCredentials: true, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { httpClientForCredentials } from '../BaseUrl'; | ||
import { LoginData } from '../../types/LoginData'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
|
||
export const onLogInSuccess = async (response: AxiosResponse) => { | ||
const { accessToken, expiresIn } = await response.data.data; | ||
|
||
httpClientForCredentials.defaults.headers.common['Authorization'] = | ||
`Bearer ${accessToken}`; | ||
|
||
const currentTime = new Date(Date.now()); | ||
const tokenExpirationTime = new Date(Date.now() + expiresIn * 1000); | ||
const refreshTime = tokenExpirationTime.getTime() - currentTime.getTime(); | ||
console.log('accessToken', response.data.message); | ||
|
||
setTimeout(() => { | ||
FetchRefreshToken(); | ||
}, refreshTime); | ||
}; | ||
|
||
export const FetchRefreshToken = async () => { | ||
try { | ||
const res = await httpClientForCredentials.get('/auth/refresh'); | ||
|
||
if (res.status === 200) { | ||
onLogInSuccess(res); | ||
console.log('refresh', res.data.message); | ||
} | ||
} catch (err) { | ||
const axiosError = err as AxiosError; | ||
if (axiosError.response?.status === 401) { | ||
alert('로그인 해주세요'); | ||
} | ||
|
||
throw new Error(); | ||
} | ||
}; | ||
|
||
export const FetchLogIn = async (data: LoginData) => { | ||
try { | ||
const response = await httpClientForCredentials.post('/auth/login', data); | ||
|
||
if (response.status === 200) { | ||
onLogInSuccess(response); | ||
} | ||
} catch (err) { | ||
console.log('로그인 에러 발생', err); | ||
throw new Error(); | ||
} | ||
}; |
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,16 @@ | ||
import { RegisterData } from '../../types/RegisterData'; | ||
import url from '../BaseUrl'; | ||
|
||
export const FetchRegister = async (register: RegisterData) => { | ||
const data = { email: register.email, password: register.password }; | ||
|
||
try { | ||
const res = await url.post('/auth/register', data); | ||
if (res.status === 201) { | ||
alert('회원가입이 완료되었습니다.이메일을 확인해주세요!!'); | ||
} | ||
} catch (error) { | ||
alert('회원가입이 실패하였습니다.'); | ||
throw new Error(); | ||
} | ||
}; |
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
File renamed without changes.
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,4 +1,4 @@ | ||
export type FormData = { | ||
username: string; | ||
export type LoginData = { | ||
email: string; | ||
password: string; | ||
}; |