summaryrefslogtreecommitdiffstats
path: root/main.js
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-10-30 13:22:06 +0300
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-10-30 13:22:06 +0300
commite24ba80820bd49af72a9e1702ed635e9b63452d1 (patch)
tree4c0479d10c94aa72f80d1dad1ad49b28c7695da9 /main.js
parente500394ed975055657fde19d3cd1c106888734b5 (diff)
Add parcel and stuff
Diffstat (limited to 'main.js')
-rw-r--r--main.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/main.js b/main.js
new file mode 100644
index 0000000..bc89f99
--- /dev/null
+++ b/main.js
@@ -0,0 +1,108 @@
+import "regenerator-runtime/runtime"
+import authConfig from "./auth_config.json";
+import { Elm } from "./src/Main.elm";
+
+// The Auth0 client, initialized in configureClient()
+let auth0 = null;
+
+/**
+ * Starts the authentication flow
+ */
+const login = async(targetUrl) => {
+ try {
+ console.log("Logging in", targetUrl);
+
+ const options = {
+ redirect_uri: window.location.origin
+ };
+
+ if (targetUrl) {
+ options.appState = { targetUrl };
+ }
+
+ await auth0.loginWithRedirect(options);
+ } catch (err) {
+ console.log("Log in failed", err);
+ }
+};
+
+/**
+ * Executes the logout flow
+ */
+const logout = () => {
+ try {
+ console.log("Logging out");
+ auth0.logout({
+ returnTo: window.location.origin
+ });
+ } catch (err) {
+ console.log("Log out failed", err);
+ }
+};
+
+/**
+ * Initializes the Auth0 client
+ */
+const configureClient = async() => {
+ auth0 = await createAuth0Client({
+ domain: authConfig.domain,
+ client_id: authConfig.clientId
+ });
+ window.auth0 = auth0;
+};
+
+// Will run when page finishes loading
+window.onload = async() => {
+ await configureClient();
+
+ // If unable to parse the history hash, default to the root URL
+ // if (!showContentFromUrl(window.location.pathname)) {
+ // showContentFromUrl("/");
+ // window.history.replaceState({ url: "/" }, {}, "/");
+ // }
+
+ const isAuthenticated = await auth0.isAuthenticated();
+
+ if (isAuthenticated) {
+ console.log("> User is authenticated");
+ window.history.replaceState({}, document.title, window.location.pathname);
+ // runElmApp();
+ return;
+ }
+
+ console.log("> User not authenticated");
+
+ const query = window.location.search;
+ const shouldParseResult = query.includes("code=") && query.includes("state=");
+
+ if (shouldParseResult) {
+ console.log("> Parsing redirect");
+ try {
+ const result = await auth0.handleRedirectCallback();
+
+ if (result.appState && result.appState.targetUrl) {
+ showContentFromUrl(result.appState.targetUrl);
+ }
+
+ console.log("Logged in!");
+ return runElmApp();
+ } catch (err) {
+ console.log("Error parsing redirect:", err);
+ }
+ }
+
+ login();
+};
+
+const runElmApp = () => {
+ try {
+ const app = Elm.Main.init({
+ node: document.getElementById("app")
+ });
+
+ app.ports.showAlert.subscribe((message) => window.alert(message));
+ app.ports.requestLogout.subscribe(logout);
+ } catch (err) {
+ console.error(err);
+ }
+} \ No newline at end of file