-
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.
Merge pull request #83 from DDD-Community/feature/82
[FEATURE/82] auth fetch
- Loading branch information
Showing
10 changed files
with
170 additions
and
39 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
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,10 +1,53 @@ | ||
import { SignInPayload, User } from '@/types' | ||
import { post } from './base' | ||
|
||
const handleResponse = async (res: Response) => { | ||
const text = await res.text() | ||
|
||
if (!res.ok) { | ||
throw new Error( | ||
`Request failed: ${res.status} - ${res.statusText} - ${text || 'No error message provided'}`, | ||
) | ||
} | ||
|
||
if (!text) { | ||
throw new Error('No response body') | ||
} | ||
|
||
try { | ||
return JSON.parse(text) | ||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
} catch (error: any) { | ||
throw new Error(`Failed to parse JSON: ${error.message}`) | ||
} | ||
} | ||
|
||
export const login = async (body: SignInPayload): Promise<User> => { | ||
const res = await post('/api/v1/oauth/sign-in', { | ||
const res = await fetch(`${process.env.API_HOST}/api/v1/oauth/sign-in`, { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
|
||
return res.data | ||
const data = await handleResponse(res) | ||
return data.data | ||
} | ||
|
||
export const refreshAT = async (refreshToken: string) => { | ||
const res = await fetch(`${process.env.API_HOST}/api/v1/oauth/re-issue`, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${refreshToken}`, | ||
}, | ||
}) | ||
|
||
const data = await handleResponse(res) | ||
|
||
return { | ||
accessToken: data.data.accessToken, | ||
refreshToken: data.data.refreshToken, | ||
expiredDate: data.data.expiredDate, | ||
} | ||
} |
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