aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2024-07-09 20:21:19 +0100
committerGravatar Joe Banks <[email protected]>2024-07-10 01:56:38 +0100
commit5dac3c06b99f42d2c571e31cbe71f14bae84d614 (patch)
treee7c15319e494f88e5e8569b0944d12b062456c29
parentWrap application in Redux Provider with new store (diff)
Send dispatches to store on authorization state attempt changes
-rw-r--r--src/api/auth.ts25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/api/auth.ts b/src/api/auth.ts
index c9e3634..2838e4b 100644
--- a/src/api/auth.ts
+++ b/src/api/auth.ts
@@ -1,10 +1,14 @@
import Cookies, { CookieSetOptions } from "universal-cookie";
import { AxiosResponse } from "axios";
+import { startAuthorizing, finishAuthorizing } from "../slices/authorization";
+import formsStore from "../store";
+
import APIClient from "./client";
const OAUTH2_CLIENT_ID = process.env.REACT_APP_OAUTH2_CLIENT_ID;
const PRODUCTION = process.env.NODE_ENV !== "development";
+const STATE_LENGTH = 64;
/**
* Authorization result as returned from the backend.
@@ -98,21 +102,36 @@ export async function getDiscordCode(scopes: OAuthScopes[], disableFunction?: (d
const cleanedScopes = ensureMinimumScopes(scopes, OAuthScopes.Identify);
// Generate a new user state
- const state = crypto.getRandomValues(new Uint32Array(1))[0];
+ const stateBytes = new Uint8Array(STATE_LENGTH);
+ crypto.getRandomValues(stateBytes);
+
+ let state = "";
+ for (let i = 0; i < stateBytes.length; i++) {
+ state += stateBytes[i].toString(16).padStart(2, "0");
+ }
const scopeString = encodeURIComponent(cleanedScopes.join(" "));
const redirectURI = encodeURIComponent(document.location.protocol + "//" + document.location.host + "/callback");
+ const windowHeight = screen.availHeight;
+ const windowWidth = screen.availWidth;
+ const requestHeight = Math.floor(windowHeight * 0.75);
+ const requestWidth = Math.floor(windowWidth * 0.4);
+
// Open login window
const windowRef = window.open(
`https://discord.com/api/oauth2/authorize?client_id=${OAUTH2_CLIENT_ID}&state=${state}&response_type=code&scope=${scopeString}&redirect_uri=${redirectURI}&prompt=consent`,
- "Discord_OAuth2"
+ "_blank",
+ `popup=true,height=${requestHeight},left=0,top=0,width=${requestWidth}`
);
+ formsStore.dispatch(startAuthorizing());
+
// Clean up on login
const interval = setInterval(() => {
if (windowRef?.closed) {
clearInterval(interval);
+ formsStore.dispatch(finishAuthorizing());
if (disableFunction) { disableFunction(false); }
}
}, 500);
@@ -133,6 +152,8 @@ export async function getDiscordCode(scopes: OAuthScopes[], disableFunction?: (d
if (message.isTrusted) {
windowRef?.close();
+ formsStore.dispatch(finishAuthorizing());
+
clearInterval(interval);
// State integrity check