Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IE11 support #7

Merged
merged 4 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed token fetching for IE11.
  • Loading branch information
Joakim Gunst committed Nov 21, 2017
commit 58e9a79f9b262650aaad8c3b5df24d90e55f6beb
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadViewer } from "./viewer";
import { getAccessToken, showUserInfo, showLogin } from "./authentication";

// Polyfills
import "core-js";
import "core-js/es6/promise";
import "whatwg-fetch";
import "url-search-params-polyfill";

Expand Down
53 changes: 26 additions & 27 deletions src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ function getAuthorizationCode() {
*/
async function getRefreshAndAccessTokens(authorizationCode: string) {
const tokenUrl = oauthUrl + "/token";
const payload = new URLSearchParams();
payload.set("grant_type", "authorization_code");
payload.set("code", authorizationCode);
payload.set("redirect_uri", redirectUri);
payload.set("client_id", clientId);
payload.set("client_secret", clientSecret);

const json = await fetchJson<AuthorizationCodeResponse>(tokenUrl, {
method: "POST",
body: payload
});

const params = new URLSearchParams();
params.set("grant_type", "authorization_code");
params.set("code", authorizationCode);
params.set("redirect_uri", redirectUri);
params.set("client_id", clientId);
params.set("client_secret", clientSecret);

const json = await fetchToken<AuthorizationCodeResponse>(tokenUrl, params);

window.localStorage.setItem("refresh_token", json.refresh_token);
window.localStorage.setItem("access_token", json.access_token);
Expand All @@ -86,17 +84,15 @@ async function getRefreshAndAccessTokens(authorizationCode: string) {
*/
async function refreshAccessToken(refreshToken: string) {
const tokenUrl = oauthUrl + "/token";
const payload = new URLSearchParams();
payload.set("grant_type", "refresh_token");
payload.set("refresh_token", refreshToken);
payload.set("redirect_uri", redirectUri);
payload.set("client_id", clientId);
payload.set("client_secret", clientSecret);

const json = await fetchJson<RefreshTokenResponse>(tokenUrl, {
method: "POST",
body: payload
});

const params = new URLSearchParams();
params.set("grant_type", "refresh_token");
params.set("refresh_token", refreshToken);
params.set("redirect_uri", redirectUri);
params.set("client_id", clientId);
params.set("client_secret", clientSecret);

const json = await fetchToken<RefreshTokenResponse>(tokenUrl, params);

window.localStorage.setItem("access_token", json.access_token);
window.localStorage.setItem(
Expand All @@ -111,11 +107,14 @@ async function refreshAccessToken(refreshToken: string) {
* Fetches and returns parsed json.
* Throws error if response is not ok.
*/
async function fetchJson<TData>(
input: RequestInfo,
init?: RequestInit | undefined
) {
const response = await fetch(input, init);
async function fetchToken<TData>(url: string, params: URLSearchParams) {
// params.toString() and explicit content-tyype header neccessary for whatwg-fetch polyfill

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: content-tyyype

const response = await fetch(url, {
method: "POST",
body: params.toString(),
headers: [["Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"]]
});

if (response.ok) {
const json: TData = await response.json();
return json;
Expand Down