import React, { useState } from "react"; import Head from "next/head"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/router"; import { faCheck, faWarning,faX } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import Aes256Gcm from "~/components/aes-256-gcm"; import Button from "~/components/basic/buttons/Button"; import InputField from "~/components/basic/InputField"; import attemptLogin from "~/utilities/attemptLogin"; import passwordCheck from "~/utilities/checks/PasswordCheck"; import issueBackupKey from "~/utilities/issueBackupKey"; import completeAccountInformationSignupInvite from "./api/auth/CompleteAccountInformationSignupInvite"; import verifySignupInvite from "./api/auth/VerifySignupInvite"; const nacl = require("tweetnacl"); const jsrp = require("jsrp"); nacl.util = require("tweetnacl-util"); const client = new jsrp.client(); const queryString = require("query-string"); export default function SignupInvite() { const [password, setPassword] = useState(""); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [firstNameError, setFirstNameError] = useState(false); const [lastNameError, setLastNameError] = useState(false); const [passwordErrorLength, setPasswordErrorLength] = useState(false); const [passwordErrorNumber, setPasswordErrorNumber] = useState(false); const [passwordErrorLowerCase, setPasswordErrorLowerCase] = useState(false); const router = useRouter(); const parsedUrl = queryString.parse(router.asPath.split("?")[1]); const [email, setEmail] = useState(parsedUrl.to); const token = parsedUrl.token; const [errorLogin, setErrorLogin] = useState(false); const [isLoading, setIsLoading] = useState(false); const [step, setStep] = useState(1); const [backupKeyError, setBackupKeyError] = useState(false); const [verificationToken, setVerificationToken] = useState(); const [backupKeyIssued, setBackupKeyIssued] = useState(false); // Verifies if the information that the users entered (name, workspace) is there, and if the password matched the criteria. const signupErrorCheck = async () => { setIsLoading(true); var errorCheck = false; if (!firstName) { setFirstNameError(true); errorCheck = true; } else { setFirstNameError(false); } if (!lastName) { setLastNameError(true); errorCheck = true; } else { setLastNameError(false); } errorCheck = passwordCheck( password, setPasswordErrorLength, setPasswordErrorNumber, setPasswordErrorLowerCase, errorCheck ); if (!errorCheck) { // Generate a random pair of a public and a private key const pair = nacl.box.keyPair(); const secretKeyUint8Array = pair.secretKey; const publicKeyUint8Array = pair.publicKey; const PRIVATE_KEY = nacl.util.encodeBase64(secretKeyUint8Array); const PUBLIC_KEY = nacl.util.encodeBase64(publicKeyUint8Array); const { ciphertext, iv, tag } = Aes256Gcm.encrypt( PRIVATE_KEY, password .slice(0, 32) .padStart( 32 + (password.slice(0, 32).length - new Blob([password]).size), "0" ) ); localStorage.setItem("PRIVATE_KEY", PRIVATE_KEY); client.init( { username: email, password: password, }, async () => { client.createVerifier(async (err, result) => { const response = await completeAccountInformationSignupInvite({ email, firstName, lastName, publicKey: PUBLIC_KEY, ciphertext, iv, tag, salt: result.salt, verifier: result.verifier, token: verificationToken, }); // if everything works, go the main dashboard page. if (!errorCheck && response.status == "200") { response = await response.json(); localStorage.setItem("publicKey", PUBLIC_KEY); localStorage.setItem( "encryptedPrivateKey", ciphertext ); localStorage.setItem("iv", iv); localStorage.setItem("tag", tag); try { await attemptLogin( email, password, setErrorLogin, router, false, false ); setStep(3); } catch (error) { setIsLoading(false); console.log("Error", error); } } }); } ); } else { setIsLoading(false); } }; // Step 4 of the sign up process (download the emergency kit pdf) const stepConfirmEmail = (

Confirm your email

verify email
); // Because this is the invite signup - we directly go to the last step of signup (email is already verified) const main = (

Almost there!

{ setPassword(password); passwordCheck( password, setPasswordErrorLength, setPasswordErrorNumber, setPasswordErrorLowerCase, false ); }} type="password" value={password} isRequired error={ passwordErrorLength && passwordErrorNumber && passwordErrorLowerCase } /> {passwordErrorLength || passwordErrorLowerCase || passwordErrorNumber ? (
Password should contain at least:
{passwordErrorLength ? ( ) : ( )}
14 characters
{passwordErrorLowerCase ? ( ) : ( )}
1 lowercase character
{passwordErrorNumber ? ( ) : ( )}
1 number
) : (
)}
); // Step 4 of the sign up process (download the emergency kit pdf) const step4 = (

Save your Emergency Kit

If you get locked out of your account, your Emergency Kit is the only way to sign in.
We recommend you download it and keep it somewhere safe.
It contains your Secret Key which we cannot access or recover for you if you lose it.
); return (
Sign Up
Infisical Wide Logo
{step == 1 ? stepConfirmEmail : step == 2 ? main : step4}
); }