"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
const icon_1 = require("@cocalc/frontend/components/icon");
const antd_1 = require("antd");
const contact_1 = __importDefault(require("components/landing/contact"));
const logo_square_1 = __importDefault(require("components/logo-square"));
const A_1 = __importDefault(require("components/misc/A"));
const post_1 = __importDefault(require("lib/api/post"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
const react_1 = require("react");
const react_google_recaptcha_v3_1 = require("react-google-recaptcha-v3");
const shared_1 = require("./shared");
const sso_1 = __importStar(require("./sso"));
function SignIn(props) {
    const { reCaptchaKey } = (0, use_customize_1.default)();
    return ((0, jsx_runtime_1.jsx)(react_google_recaptcha_v3_1.GoogleReCaptchaProvider, { reCaptchaKey: reCaptchaKey, children: (0, jsx_runtime_1.jsx)(SignIn0, { ...props }) }));
}
exports.default = SignIn;
function SignIn0(props) {
    const { minimal = false, onSuccess } = props;
    const { anonymousSignup, reCaptchaKey, siteName, strategies } = (0, use_customize_1.default)();
    const [email, setEmail] = (0, react_1.useState)("");
    const [password, setPassword] = (0, react_1.useState)("");
    const [signingIn, setSigningIn] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)("");
    const { executeRecaptcha } = (0, react_google_recaptcha_v3_1.useGoogleReCaptcha)();
    const haveSSO = strategies != null && strategies.length > 0;
    // based on email: if user has to sign up via SSO, this will tell which strategy to use.
    const requiredSSO = (0, sso_1.useRequiredSSO)(strategies, email);
    async function signIn() {
        if (signingIn)
            return;
        setError("");
        try {
            setSigningIn(true);
            let reCaptchaToken;
            if (reCaptchaKey) {
                if (!executeRecaptcha) {
                    throw Error("Please wait a few seconds, then try again.");
                }
                reCaptchaToken = await executeRecaptcha("signin");
            }
            await (0, post_1.default)("/auth/sign-in", {
                email,
                password,
                reCaptchaToken,
            });
            onSuccess?.();
        }
        catch (err) {
            setError(`${err}`);
        }
        finally {
            setSigningIn(false);
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", { style: { margin: "30px", minHeight: "50vh" }, children: [!minimal && ((0, jsx_runtime_1.jsxs)("div", { style: { textAlign: "center", marginBottom: "15px" }, children: [(0, jsx_runtime_1.jsx)(logo_square_1.default, { style: { width: "100px", height: "100px", marginBottom: "15px" }, priority: true }), (0, jsx_runtime_1.jsxs)("h1", { children: ["Sign In to ", siteName] })] })), (0, jsx_runtime_1.jsxs)("div", { style: shared_1.LOGIN_STYLE, children: [(0, jsx_runtime_1.jsx)("div", { style: { margin: "10px 0" }, children: strategies == null
                            ? "Sign in"
                            : haveSSO
                                ? requiredSSO != null
                                    ? "Sign in using your single sign-on provider"
                                    : "Sign in using your email address or a single sign-on provider."
                                : "Sign in using your email address." }), (0, jsx_runtime_1.jsxs)("form", { children: [haveSSO && ((0, jsx_runtime_1.jsx)("div", { style: {
                                    textAlign: "center",
                                    margin: "20px 0",
                                    display: requiredSSO == null ? "inherit" : "none",
                                }, children: (0, jsx_runtime_1.jsx)(sso_1.default, { size: email ? 24 : undefined, style: email
                                        ? { textAlign: "right", marginBottom: "20px" }
                                        : undefined }) })), (0, jsx_runtime_1.jsx)(antd_1.Input, { autoFocus: true, style: { fontSize: "12pt" }, placeholder: "Email address", autoComplete: "username", onChange: (e) => setEmail(e.target.value) }), (0, jsx_runtime_1.jsx)(sso_1.RequiredSSO, { strategy: requiredSSO }), (0, jsx_runtime_1.jsxs)("div", { style: {
                                    marginTop: "30px",
                                    display: requiredSSO == null ? "inherit" : "none",
                                }, children: [(0, jsx_runtime_1.jsx)("p", { children: "Password " }), (0, jsx_runtime_1.jsx)(antd_1.Input.Password, { style: { fontSize: "12pt" }, autoComplete: "current-password", placeholder: "Password", onChange: (e) => setPassword(e.target.value), onPressEnter: (e) => {
                                            e.preventDefault();
                                            signIn();
                                        } })] }), email && requiredSSO == null && ((0, jsx_runtime_1.jsx)(antd_1.Button, { disabled: signingIn || !(password?.length >= 6), shape: "round", size: "large", type: "primary", style: { width: "100%", marginTop: "20px" }, onClick: signIn, children: signingIn ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "spinner", spin: true }), " Signing In..."] })) : !password || password.length < 6 ? ("Enter your password above.") : ("Sign In") }))] }), error && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { marginTop: "20px" }, message: "Error", description: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("p", { children: (0, jsx_runtime_1.jsx)("b", { children: error }) }), (0, jsx_runtime_1.jsxs)("p", { children: ["If you can't remember your password,", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/password-reset", children: "reset it" }), ". If that doesn't work ", (0, jsx_runtime_1.jsx)(contact_1.default, {}), "."] })] }), type: "error", showIcon: true }), (0, jsx_runtime_1.jsx)("div", { style: {
                                    textAlign: "center",
                                    marginTop: "15px",
                                    fontSize: "14pt",
                                }, children: (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/password-reset", children: "Forgot password?" }) })] }))] }), !minimal && ((0, jsx_runtime_1.jsxs)("div", { style: {
                    ...shared_1.LOGIN_STYLE,
                    backgroundColor: "white",
                    margin: "30px auto",
                    padding: "15px",
                }, children: ["New to ", siteName, "? ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-up", children: "Sign Up" }), anonymousSignup && ((0, jsx_runtime_1.jsxs)("div", { style: { marginTop: "15px" }, children: ["Don't want to provide any information?", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)(A_1.default, { href: "/auth/try", children: ["Try ", siteName, " without creating an account."] })] }))] }))] }));
}
//# sourceMappingURL=sign-in.js.map