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 Button from '~/components/basic/buttons/Button'; import InputField from '~/components/basic/InputField'; import Aes256Gcm from '~/components/utilities/cryptography/aes-256-gcm'; import issueBackupKey from '~/components/utilities/cryptography/issueBackupKey'; import attemptLogin from '~/utilities/attemptLogin'; import passwordCheck from '~/utilities/checks/PasswordCheck'; 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?.replace(' ', '+').trim()); 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({ text: PRIVATE_KEY, secret: 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) => { let 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
Almost there!
Save your Emergency Kit