Read the config in checkForToken

This commit is contained in:
Marquis Kurt 2020-01-21 10:41:45 -05:00
parent 8d14ff78df
commit a46d9c6c0f
No known key found for this signature in database
GPG Key ID: 725636D259F5402D
1 changed files with 65 additions and 36 deletions

View File

@ -649,43 +649,72 @@ class WelcomePage extends Component<IWelcomeProps, IWelcomeState> {
let clientLoginSession: SaveClientSession = JSON.parse(
loginData
);
Mastodon.fetchAccessToken(
clientLoginSession.clientId,
clientLoginSession.clientSecret,
code,
localStorage.getItem("baseurl") as string,
this.state.emergencyMode
? undefined
: clientLoginSession.authUrl.includes(
"urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob"
)
? undefined
: window.location.protocol === "hyperspace:"
? "hyperspace://hyperspace/app/"
: `https://${window.location.host}`
)
// If we succeeded, store the access token and redirect to the
// main view.
.then((tokenData: any) => {
localStorage.setItem(
"access_token",
tokenData.access_token
);
this.redirectToApp();
})
// Otherwise, present an error
.catch((err: Error) => {
this.props.enqueueSnackbar(
`Couldn't authorize ${
this.state.brandName
? this.state.brandName
: "Hyperspace"
}: ${err.name}`,
{ variant: "error" }
);
console.error(err.message);
});
// Re-read the config file. It's possible the state doesn't take
// effect immediately.
getConfig().then((result: any) => {
if (result !== undefined) {
let config: Config = result;
let redirectUrl =
`https://${window.location.host}` || undefined;
// Is this an emergency login? Don't pass a redirect
// URI
if (
this.state.emergencyMode ||
clientLoginSession.authUrl.includes(
"urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob"
)
) {
redirectUrl = undefined;
}
// Is this the desktop app? Change the redirect URI
else if (window.location.protocol === "hyperspace:") {
redirectUrl = "hyperspace://hyperspace/app/";
}
// Otherwise, read the config
else if (
config.location !== "dynamic" &&
config.location !== "desktop" &&
!inDisallowedDomains(config.location)
) {
redirectUrl = config.location;
}
// Fetch the access token
Mastodon.fetchAccessToken(
clientLoginSession.clientId,
clientLoginSession.clientSecret,
code,
localStorage.getItem("baseurl") as string,
redirectUrl
)
// If we succeeded, store the access token and redirect to the
// main view.
.then((tokenData: any) => {
localStorage.setItem(
"access_token",
tokenData.access_token
);
this.redirectToApp();
})
// Otherwise, present an error
.catch((err: Error) => {
this.props.enqueueSnackbar(
`Couldn't authorize ${
this.state.brandName
? this.state.brandName
: "Hyperspace"
}: ${err.name}`,
{ variant: "error" }
);
console.error(err.message);
});
}
});
}
}
}