/* eslint-disable react/no-multi-comp */ import React, { useState, useEffect } from "react"; import { makeStyles } from "@mui/styles"; import { useInterval } from "react-powerhooks"; import theme from '../theme.jsx'; import { CircularProgress, TextField, Button, Paper, Typography, } from "@mui/material"; import { useNavigate } from "react-router-dom"; const hrefStyle = { color: "white", textDecoration: "none", }; const bodyDivStyle = { margin: "auto", marginTop: 150, width: "500px", }; const useStyles = makeStyles({ notchedOutline: { borderColor: "#f85a3e !important", }, }); const LoginDialog = (props) => { const { globalUrl, isLoaded, isLoggedIn, setIsLoggedIn, setCookie, register, checkLogin, } = props; let navigate = useNavigate(); const classes = useStyles(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [firstRequest, setFirstRequest] = useState(true); const [loginLoading, setLoginLoading] = useState(false); const [loginViewLoading, setLoginViewLoading] = useState(false); const [ssoUrl, setSSOUrl] = useState(""); const [MFAField, setMFAField] = useState(false); const [MFAValue, setMFAValue] = useState(""); // Used to swap from login to register. True = login, false = register useEffect(() => { checkAdmin() }, [loginViewLoading]) // Error messages etc const [loginInfo, setLoginInfo] = useState(""); const handleValidateForm = () => { return username.length > 1 && password.length > 1; }; if (isLoggedIn === true) { //window.location.pathname = "/workflows"; navigate("/workflows") } const checkAdmin = () => { const url = globalUrl + "/api/v1/checkusers"; fetch(url, { method: "GET", headers: { "Content-Type": "application/json", }, }) .then((response) => response.json().then((responseJson) => { if (responseJson["success"] === false) { setLoginInfo(responseJson["reason"]); } else { if (responseJson.sso_url !== undefined && responseJson.sso_url !== null) { setSSOUrl(responseJson.sso_url); } navigate("/login") if (loginViewLoading) { setLoginViewLoading(false); checkLogin(); stop(); if ( responseJson.reason !== undefined && responseJson.reason !== null ) { setLoginInfo(responseJson.reason); } } if (responseJson.reason === "stay") { navigate("/adminsetup") } } }) ) .catch((error) => { if (!loginViewLoading) { setLoginViewLoading(true); start(); } }); }; const { start, stop } = useInterval({ duration: 3000, startImmediate: false, callback: () => { checkAdmin(); }, }) if (firstRequest) { setFirstRequest(false); checkAdmin(); } const onSubmit = (e) => { setLoginLoading(true); e.preventDefault(); setLoginInfo(""); // FIXME - add some check here ROFL // Just use this one? var data = { username: username, password: password }; if (MFAValue !== undefined && MFAValue !== null && MFAValue.length > 0) { data["mfa_code"] = MFAValue; } var baseurl = globalUrl; if (register) { var url = baseurl + "/api/v1/login"; fetch(url, { mode: "cors", method: "POST", body: JSON.stringify(data), credentials: "include", crossDomain: true, withCredentials: true, headers: { "Content-Type": "application/json; charset=utf-8", }, }) .then((response) => response.json().then((responseJson) => { setLoginLoading(false); if (responseJson["success"] === false) { setLoginInfo(responseJson["reason"]); } else { if (responseJson["reason"] === "MFA_REDIRECT") { setLoginInfo( "MFA required. Please enter the 6-digit code from your authenticator" ); setMFAField(true); return; } else if (responseJson["reason"] === "MFA_SETUP") { window.location.href = `/login/${responseJson.url}/mfa-setup`; return; } setLoginInfo("Successful login, rerouting"); for (var key in responseJson["cookies"]) { setCookie( responseJson["cookies"][key].key, responseJson["cookies"][key].value, { path: "/" } ); } if (responseJson.tutorials === undefined || responseJson.tutorials === null || !responseJson.tutorials.includes("welcome")) { console.log("RUN Welcome!!") setTimeout(() => { navigate("/welcome?tab=2") },200) // window.location.pathname = "" return } const tmpView = new URLSearchParams(window.location.search).get("view") if (tmpView !== undefined && tmpView !== null) { //const newUrl = `/${tmpView}${decodeURIComponent(window.location.search)}` const newUrl = `/${tmpView}` window.location.pathname = newUrl } else { window.location.pathname = "/workflows" } setIsLoggedIn(true); } }) ) .catch((error) => { setLoginLoading(false); setLoginInfo("Error logging in: " + error); }); } else { url = baseurl + "/api/v1/users/register"; fetch(url, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json", }, }) .then((response) => response.json().then((responseJson) => { if (responseJson["success"] === false) { setLoginInfo(responseJson["reason"]); } else { setLoginInfo("Successful register!"); } }) ) .catch((error) => { setLoginInfo("Error in from backend: ", error); }); } }; const onChangeUser = (e) => { setUsername(e.target.value); }; const onChangePass = (e) => { setPassword(e.target.value); }; //const onClickRegister = () => { // if (props.location.pathname === "/login") { // window.location.pathname = "/register" // } else { // window.location.pathname = "/login" // } // setLoginCheck(!register) //} //var loginChange = register ? (

Want to register? Click here.

) : (

Go back to login? Click here.

); var formtitle = register ?
Login
:
Register
; const imgsize = 100; const basedata = (
{loginViewLoading ? (
Waiting for the Shuffle database to become available. This may take up to two minutes. {loginInfo === undefined || loginInfo === null || loginInfo.length === 0 ? null : (
Database Response: {loginInfo}
)} Are you sure Shuffle is{" "} installed correctly ? 1. Make sure shuffle-database folder has correct access, and that you have a minimum of 2Gb of RAM available:{" "}

sudo chown -R 1000:1000 shuffle-database
2. Disable memory swap on the host:

sudo swapoff -a
3. Restart the database:

sudo docker restart shuffle-opensearch
Need help?{" "} Join the Discord!
) : (

{formtitle}

Username
Password
{MFAField === true ? (
2-factor code { setMFAValue(event.target.value); }} />
) : null}
{loginInfo}
{ssoUrl !== undefined && ssoUrl !== null && ssoUrl.length > 0 ? (
Or
) : null}
)}
); const loadedCheck = isLoaded ?
{basedata}
:
; useEffect(() => { setTimeout(() => { if (ssoUrl !== undefined && ssoUrl !== null && ssoUrl.length > 0) { //id="sso_button" const ssoBtn = document.getElementById("sso_button"); if (ssoBtn !== undefined && ssoBtn !== null) { //console.log("SSO BTN: ", ssoBtn) const cursearch = typeof window === "undefined" || window.location === undefined ? "" : window.location.search; var tmpView = new URLSearchParams(cursearch).get("autologin"); if (tmpView !== undefined && tmpView !== null) { if (tmpView === "true") { console.log("Tmp: ", tmpView) ssoBtn.click() } } } } }, 200); }, [ssoUrl]) return
{loadedCheck}
; }; export default LoginDialog;