import React, { useEffect, useState } from "react"; import PropTypes from "prop-types"; import * as util from "../../util"; // Styles import "./Auth.css"; const SignIn = (props) => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [validEmail, setValidEmail] = useState(true); const [processing, setProcessing] = useState(false); const [unauthorized, setUnauthorized] = useState(false); const inputRef = React.createRef(); useEffect(() => { document.addEventListener("keydown", handleKeyDown); inputRef.current.focus(); return () => { document.removeEventListener("keydown", handleKeyDown); }; }, []); const signIn = async () => { try { const baseUrl = util.getApiUrlBase(); const url = `${baseUrl}auth`; const options = { method: "POST", body: JSON.stringify({ email: email, password: password, }), }; const response = await fetch(url, options); const json = await response.json(); setUnauthorized(false); setProcessing(false); util.setWithExpiry( `ugc`, json.AuthenticationResult, json.AuthenticationResult.ExpiresIn ); props.setUserAuth(json.AuthenticationResult); props.getUserInfo(json.AuthenticationResult); props.getUserStreamInfo(json.AuthenticationResult); props.closeSignIn(); } catch (error) { console.log("Signin failed: ", error); setUnauthorized(true); setProcessing(false); } }; const handleKeyDown = (e) => { if (e.keyCode === 27) { // keyCode 27 is Escape key props.closeSignIn(); } }; const handleEmailChange = (e) => { setEmail(e.target.value); }; const handlePasswordChange = (e) => { setPassword(e.target.value); }; const handleLinkClick = (e) => { e.preventDefault(); props.closeSignIn(); props.showSignUp(); }; const resetSignIn = () => { setValidEmail(true); setProcessing(false); setUnauthorized(false); }; const handleSignIn = (e) => { e.preventDefault(); const validEmail = util.validateEmail(email); if (validEmail) { setProcessing(true); signIn(); } resetSignIn(); setValidEmail(validEmail); }; const signInDisabled = !email || !password || processing; const signInText = processing ? "Processing..." : "Sign In"; return (