Skip to content

Commit

Permalink
fix: fix login bug (#17)
Browse files Browse the repository at this point in the history
* fix: fix login

* Update sdk.js

---------

Co-authored-by: Eric Luo <[email protected]>
  • Loading branch information
SamYSF and hsluoyz authored Mar 3, 2024
1 parent cd2a326 commit 4e214af
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,26 @@ class Sdk {
getSignInUrl(clientId) {
const endpoint = this.config.endpoint;
const redirectUri = this.getRedirectUri();
const scope = "profile";
const scope = "openid";
const state = this.config.applicationName;
return `${endpoint}/login/oauth/authorize?client_id=${clientId}&response_type=token&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`;
}

getAccessTokenFromRedirectUrl(redirectUrl) {
if (redirectUrl) {
const hashTagIndex = redirectUrl.indexOf("#");
const questionMaskIndex = redirectUrl.indexOf("?");
if (
hashTagIndex > 0 &&
questionMaskIndex > 0 &&
hashTagIndex < questionMaskIndex
) {
// e.g. "https://${extensionId}.chromiumapp.org#token=${accessToken}?state=${state}&token_type=bearer"
return redirectUrl.substring(hashTagIndex + 7, questionMaskIndex);
}
const accessTokenMatch = redirectUrl.match(/#access_token=([^&]*)/);
const accessToken = accessTokenMatch ? accessTokenMatch[1] : "";
return accessToken;
}
return "";
}

login(func) {
this.getApplication().then(application => {
this.getApplication().then((res) => {
// refer: https://developer.chrome.com/docs/extensions/reference/identity/#method-launchWebAuthFlow
chrome.identity.launchWebAuthFlow(
{
url: this.getSignInUrl(application.clientId),
url: this.getSignInUrl(res.data.clientId),
interactive: true,
},
(redirectUrl) => {
Expand Down

0 comments on commit 4e214af

Please sign in to comment.