-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimize github/gitee token process (#919)
* feat: optimize github token process Signed-off-by: frank-zsy <[email protected]> * feat: add gitee token process Signed-off-by: frank-zsy <[email protected]> * fix: use callback to server directly Signed-off-by: frank-zsy <[email protected]> * fix: fix error message Signed-off-by: frank-zsy <[email protected]> * refactor: update UI Signed-off-by: frank-zsy <[email protected]> * fix: update auth scope Signed-off-by: frank-zsy <[email protected]> * update oauth url Signed-off-by: frank-zsy <[email protected]> * Update fast pr config rule file path Signed-off-by: frank-zsy <[email protected]> --------- Signed-off-by: frank-zsy <[email protected]>
- Loading branch information
Showing
11 changed files
with
224 additions
and
276 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,11 +1,43 @@ | ||
export const saveGiteeToken = (token: string) => { | ||
chrome.storage.sync.set({ gitee_token: token }); | ||
}; | ||
const GITEE_TOKEN_KEY = 'gitee_token'; | ||
|
||
export const getGiteeToken = (): Promise<string | null> => { | ||
return new Promise((resolve) => { | ||
chrome.storage.sync.get('gitee_token', (result) => { | ||
resolve(result.gitee_token || null); | ||
}); | ||
export const saveGiteeToken = (token: string, expireAt: number, refreshToken: string) => { | ||
return chrome.storage.sync.set({ | ||
[GITEE_TOKEN_KEY]: { | ||
token, | ||
expireAt, | ||
refreshToken, | ||
}, | ||
}); | ||
}; | ||
|
||
export const getGiteeToken = async (): Promise<string | null> => { | ||
const result = await chrome.storage.sync.get(GITEE_TOKEN_KEY); | ||
if (!result || !result[GITEE_TOKEN_KEY]) { | ||
return null; | ||
} | ||
const tokenInfo = result[GITEE_TOKEN_KEY]; | ||
if (!tokenInfo.expireAt || tokenInfo.expireAt > Date.now()) { | ||
return tokenInfo.token || null; | ||
} else { | ||
console.log('Gitee token expired and need refesh'); | ||
const refreshReq = await fetch( | ||
`https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token=${tokenInfo.refreshToken}`, | ||
{ method: 'POST' } | ||
); | ||
const refreshData = await refreshReq.json(); | ||
if (!refreshData) { | ||
console.log('Gitee token refresh failed'); | ||
return null; | ||
} | ||
await saveGiteeToken( | ||
refreshData.access_token, | ||
Date.now() + (refreshData.expires_in - 120) * 1000, | ||
refreshData.refresh_token | ||
); | ||
return refreshData.access_token; | ||
} | ||
}; | ||
|
||
export const removeGiteeToken = () => { | ||
return chrome.storage.sync.remove(GITEE_TOKEN_KEY); | ||
}; |
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,11 +1,17 @@ | ||
const GITHUB_TOKEN_KEY = 'github_token'; | ||
|
||
export const saveGithubToken = (token: string) => { | ||
chrome.storage.sync.set({ github_token: token }); | ||
return chrome.storage.sync.set({ [GITHUB_TOKEN_KEY]: token }); | ||
}; | ||
|
||
export const getGithubToken = (): Promise<string | null> => { | ||
return new Promise((resolve) => { | ||
chrome.storage.sync.get('github_token', (result) => { | ||
resolve(result.github_token || null); | ||
chrome.storage.sync.get(GITHUB_TOKEN_KEY, (result) => { | ||
resolve(result[GITHUB_TOKEN_KEY] || null); | ||
}); | ||
}); | ||
}; | ||
|
||
export const removeGithubToken = () => { | ||
return chrome.storage.sync.remove(GITHUB_TOKEN_KEY); | ||
}; |
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
Oops, something went wrong.