-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
238 additions
and
127 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 |
---|---|---|
@@ -1,6 +1,26 @@ | ||
import { AccessTokenError } from "../../errors" | ||
|
||
export async function getAccessToken() { | ||
// TODO: cache response and invalidate according to expiresAt | ||
const tokenRes = await fetch("/auth/access-token").then((res) => res.json()) | ||
const tokenRes = await fetch("/auth/access-token") | ||
|
||
if (!tokenRes.ok) { | ||
// try to parse it as JSON and throw the error from the API | ||
// otherwise, throw a generic error | ||
let accessTokenError | ||
try { | ||
accessTokenError = await tokenRes.json() | ||
} catch (e) { | ||
throw new Error( | ||
"An unexpected error occurred while trying to fetch the access token." | ||
) | ||
} | ||
|
||
throw new AccessTokenError( | ||
accessTokenError.error.code, | ||
accessTokenError.error.message | ||
) | ||
} | ||
|
||
return tokenRes.token | ||
const tokenSet = await tokenRes.json() | ||
return tokenSet.token | ||
} |
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.