UNPKG

@memori.ai/memori-react

Version:

[![npm version](https://img.shields.io/github/package-json/v/memori-ai/memori-react)](https://www.npmjs.com/package/@memori.ai/memori-react) ![Tests](https://github.com/memori-ai/memori-react/workflows/CI/badge.svg?branch=main) ![TypeScript Support](https

196 lines 13.9 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useEffect, useState } from 'react'; import Button from '../ui/Button'; import { useTranslation } from 'react-i18next'; import toast from 'react-hot-toast'; import { mailRegEx, pwdRegEx, usernameRegEx } from '../../helpers/utils'; import { getErrori18nKey } from '../../helpers/error'; const reservedUserNames = [ 'memori', 'private', 'public', 'api', '404', '500', 'it', 'en', 'account', 'auth', 'logout', 'privacy_and_cookie', 'unauthorized', 'users', 'virtual_spaces', ]; const SignupForm = ({ tenant, apiClient, onLogin, goToLogin, __TEST__waitingForOtp = false, }) => { const { t, i18n } = useTranslation(); const lang = i18n.language === 'it' ? 'it' : 'en'; const { userSignUp, userConfirmSignUp, resendVerificationCode } = apiClient.backend; const [email, setEmail] = useState(); const [username, setUsername] = useState(); const [password, setPassword] = useState(); const [confirmPassword, setConfirmPassword] = useState(); const pwdAcceptable = !password || (password && pwdRegEx.test(password)); const pwdGreen = pwdAcceptable && password && password.length >= 24; const pwdEmpty = !password || password.length === 0; const pwdMeterValue = !pwdAcceptable || pwdEmpty ? 0 : password.length < 8 ? 15 : password.length >= 32 ? 100 : (password.length - 8) * (50 / 24) + 50; const [loading, setLoading] = useState(false); const [loadingOtp, setLoadingOtp] = useState(false); const [waitingForOtp, setWaitingForOtp] = useState(__TEST__waitingForOtp); const [error, setError] = useState(null); const [referral, setReferral] = useState(); useEffect(() => { setReferral(window.location.href); }, []); const generateUsernameFromEmail = (email) => email.split('@')[0].replace(/[^a-zA-Z0-9]/g, ''); const signup = async (e) => { var _a, _b, _c, _d, _e, _f, _g; e.preventDefault(); const form = e.currentTarget; const tenantID = (_a = form.tenant.value) !== null && _a !== void 0 ? _a : tenant.name; const eMail = ((_b = form.eMail.value) !== null && _b !== void 0 ? _b : email).toLowerCase(); const userName = (_c = form.userName.value) !== null && _c !== void 0 ? _c : username; const password = form.password.value; const birthDate = new Date((_d = form.birthDate) === null || _d === void 0 ? void 0 : _d.value).toISOString(); const tnCAndPPAccepted = (_e = form.tnCAndPPAccepted) === null || _e === void 0 ? void 0 : _e.checked; const pAndCUAccepted = (_f = form.pAndCUAccepted) === null || _f === void 0 ? void 0 : _f.checked; const referral = (_g = form.referral) === null || _g === void 0 ? void 0 : _g.value; if (!eMail || !userName || !password || !birthDate || !tnCAndPPAccepted) { setError(t('missingData')); return; } setLoading(true); setError(null); const age = (new Date().getTime() - new Date(birthDate).getTime()) / 1000 / 60 / 60 / 24 / 365; if (age < 14) { toast.error(t('login.underage', { age: 14 })); setError(t('login.underage', { age: 14 })); return; } try { const { user, ...resp } = await userSignUp({ tenant: tenantID, eMail, userName, password, birthDate, tnCAndPPAccepted, tnCAndPPAcceptanceDate: new Date().toISOString(), pAndCUAccepted, pAndCUAcceptanceDate: new Date().toISOString(), referral, }); if (resp.resultCode !== 0) { console.error(resp); toast.error(t(getErrori18nKey(resp.resultCode))); } else if (user) { toast.success(t('success')); setWaitingForOtp(true); } } catch (e) { let err = e; console.error('[signup]', err); if (err === null || err === void 0 ? void 0 : err.message) toast.error(err.message); } finally { setLoading(false); } }; const resendOtp = async (e) => { e.preventDefault(); if (!username) return; setLoadingOtp(true); try { const resp = await resendVerificationCode({ tenant: tenant === null || tenant === void 0 ? void 0 : tenant.name, userName: username, }); if (resp.resultCode === 0) { toast.success(t('resentVerificationCode')); } else { console.error(resp); toast.error(t(getErrori18nKey(resp.resultCode))); } } catch (e) { let err = e; console.error('[resend otp]', err); if (err === null || err === void 0 ? void 0 : err.message) toast.error(err.message); } finally { setLoadingOtp(false); } }; const validateOtp = async (e) => { var _a, _b, _c; e.preventDefault(); const form = e.currentTarget; const tenantID = (_a = form.tenant.value) !== null && _a !== void 0 ? _a : tenant.name; const userName = (_b = form.userName.value) !== null && _b !== void 0 ? _b : username; const pwd = (_c = form.password.value) !== null && _c !== void 0 ? _c : password; const verificationCode = form.verificationCode.value; setLoading(true); setError(null); try { const { user, token, ...resp } = await userConfirmSignUp({ tenant: tenantID, userName, password: pwd, verificationCode, }); if (resp.resultCode !== 0) { console.error(resp); toast.error(t(getErrori18nKey(resp.resultCode))); } else if (user && token) { toast.success(t('success')); onLogin(user, token); } } catch (e) { let err = e; console.error('[validate otp]', err); if (err === null || err === void 0 ? void 0 : err.message) toast.error(err.message); } finally { setLoading(false); } }; return waitingForOtp ? (_jsxs(_Fragment, { children: [_jsxs("form", { className: "memori--login-drawer--form", onSubmit: validateOtp, children: [_jsx("input", { type: "hidden", name: "userName", value: username }), _jsx("input", { type: "hidden", name: "password", value: password }), _jsx("input", { type: "hidden", name: "tenant", value: tenant.name }), _jsxs("label", { htmlFor: "#verificationCode", children: [t('login.otpCode'), _jsx("input", { id: "verificationCode", name: "verificationCode", type: "text", required: true, autoComplete: "one-time-code", placeholder: t('login.otpCode') || 'OTP' })] }), _jsx("p", { children: _jsx(Button, { outlined: true, onClick: resendOtp, loading: loadingOtp, children: t('login.resendVerificationCode') }) }), _jsx(Button, { htmlType: "submit", primary: true, loading: loading, children: t('confirm') })] }), error && (_jsx("p", { role: "alert", className: "memori--login-drawer--error", children: error }))] })) : (_jsxs(_Fragment, { children: [_jsxs("form", { className: "memori--login-drawer--form", onSubmit: signup, children: [_jsx("input", { type: "hidden", name: "tenant", value: tenant.name }), _jsx("input", { type: "hidden", name: "referral", value: referral }), _jsxs("label", { htmlFor: "#eMail", children: [t('login.email'), _jsx("input", { type: "email", name: "eMail", id: "eMail", required: true, autoComplete: "email", placeholder: t('login.email') || 'Email', onChange: e => setEmail(e.target.value), "aria-invalid": !!(email === null || email === void 0 ? void 0 : email.length) && !mailRegEx.test(email) })] }), !!(email === null || email === void 0 ? void 0 : email.length) && !mailRegEx.test(email) && (_jsx("p", { className: "memori--login-drawer--inline-error", children: t('login.emailFormatError') })), _jsxs("label", { htmlFor: "userName", children: [t('login.username'), _jsx("input", { type: "text", name: "userName", id: "userName", required: true, autoComplete: "username", placeholder: t('login.username') || 'Username', onFocus: () => { if (!(username === null || username === void 0 ? void 0 : username.length) && !!(email === null || email === void 0 ? void 0 : email.length)) { let username = generateUsernameFromEmail(email); const field = document.getElementById('userName'); if (field) { field.value = username; setUsername(username); } } }, onChange: e => setUsername(e.target.value), "aria-invalid": !!(username === null || username === void 0 ? void 0 : username.length) && (reservedUserNames.includes(username.toLowerCase()) || !usernameRegEx.test(username)) })] }), !!(username === null || username === void 0 ? void 0 : username.length) && reservedUserNames.includes(username.toLowerCase()) && (_jsx("p", { className: "memori--login-drawer--inline-error", children: t('login.usernameContainsReservedWord') })), !!(username === null || username === void 0 ? void 0 : username.length) && !usernameRegEx.test(username) && (_jsx("p", { className: "memori--login-drawer--inline-error", children: t('login.usernameFormatError') })), _jsxs("label", { htmlFor: "#birthDate", children: [t('login.birthDate'), _jsx("input", { id: "birthDate", name: "birthDate", type: "date", required: true, autoComplete: "bday" })] }), _jsx("p", { children: _jsx("small", { children: t('login.birthDateHelper') }) }), _jsxs("label", { htmlFor: "#password", children: [t('login.password'), _jsx("input", { id: "password", name: "password", type: "password", required: true, autoComplete: "new-password", placeholder: t('login.password') || 'Password', onChange: e => setPassword(e.target.value), "aria-invalid": !pwdAcceptable })] }), !pwdAcceptable && (_jsx("p", { className: "memori--login-drawer--inline-error", children: t('login.passwordFormatError') })), _jsxs("label", { htmlFor: "#confirm-password", children: [t('login.confirmPassword'), _jsx("input", { id: "confirm-password", name: "confirmPassword", type: "password", required: true, autoComplete: "new-password", placeholder: t('login.confirmPassword') || 'Password', onChange: e => setConfirmPassword(e.target.value), "aria-invalid": !!(password === null || password === void 0 ? void 0 : password.length) && !!(confirmPassword === null || confirmPassword === void 0 ? void 0 : confirmPassword.length) && password !== confirmPassword })] }), !!(password === null || password === void 0 ? void 0 : password.length) && !!(confirmPassword === null || confirmPassword === void 0 ? void 0 : confirmPassword.length) && password !== confirmPassword && (_jsx("p", { className: "memori--login-drawer--inline-error", children: t('login.passwordMatchingError') })), _jsx("meter", { className: "memori--login-drawer--password-meter", min: 0, low: 33, high: 66, optimum: 80, max: 100, value: pwdMeterValue, id: "password-strength-meter" }), _jsx("small", { children: t(`login.pwd${pwdGreen ? 'Strong' : pwdAcceptable ? 'Acceptable' : 'Weak'}`) }), _jsxs("label", { className: "memori-checkbox", children: [_jsxs("span", { className: "memori-checkbox--input-wrapper", children: [_jsx("input", { type: "checkbox", name: "tnCAndPPAccepted", required: true, className: "memori-checkbox--input" }), _jsx("span", { className: "memori-checkbox--inner" })] }), _jsxs("span", { className: "memori-checkbox--text", children: [t('login.privacyLabel'), ' ', _jsx("a", { href: `https://memori.ai/${lang}/privacy_and_cookie`, target: "_blank", rel: "noopener noreferrer", children: t('login.privacyAndCookiePolicy') }), ' ', t('login.and'), ' ', _jsx("a", { href: `https://memori.ai/${lang}/tos`, target: "_blank", rel: "noopener noreferrer", children: t('login.termsOfService') })] })] }), _jsxs("label", { className: "memori-checkbox", children: [_jsxs("span", { className: "memori-checkbox--input-wrapper", children: [_jsx("input", { type: "checkbox", name: "pAndCUAccepted", className: "memori-checkbox--input" }), _jsx("span", { className: "memori-checkbox--inner" })] }), _jsxs("span", { className: "memori-checkbox--text", children: [t('login.pAndCUAccepted'), ' ', _jsx("small", { children: _jsxs("em", { children: ["(", t('login.optional'), ")"] }) })] })] }), _jsx("p", { children: _jsx("small", { children: t('login.deepThoughtExplaination') }) }), _jsx(Button, { htmlType: "submit", primary: true, loading: loading, children: t('login.signUp') }), _jsxs("p", { className: "memori--login-drawer--signup", children: [t('login.alreadyHaveAnAccount'), ' ', _jsx(Button, { outlined: true, onClick: goToLogin, children: t('login.backToLogin') })] })] }), error && (_jsx("p", { role: "alert", className: "memori--login-drawer--error", children: error }))] })); }; export default SignupForm; //# sourceMappingURL=SignupForm.js.map