diff --git a/bfrontend/src/Components/Auth/Create.js b/bfrontend/src/Components/Auth/Create.js new file mode 100644 index 0000000..e9e26d8 --- /dev/null +++ b/bfrontend/src/Components/Auth/Create.js @@ -0,0 +1,95 @@ +import { useState } from 'react'; +import { useHistory } from 'react-router-dom'; + +import Notification from '../UI/Notification'; +import APIRequest from '../../API/APIRequest'; +import { getSignupMessageFromError } from '../../Util/Errors' + +export default function Create() { + const history = useHistory(); + + const [ usernameInput, setUsernameInput ] = useState(); + const [ passwordInput, setPasswordInput ] = useState(); + const [ specialCodeInput, setSpecialCodeInput ] = useState(); + + const [ specialCodePrompt, setSpecialCodePrompt ] = useState(); + + const [ info, setInfo ] = useState(); + + const doCreateAccount = async () => { + const { json, isOK } = await APIRequest('/api/v1/users/account/create', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + username: usernameInput, + password: passwordInput, + email: "user@example.com", + specialCode: specialCodeInput + }) + }); + + if (!isOK && json) { + setInfo(getSignupMessageFromError(json)); + return; + } + if (!isOK) { + setInfo('Something went wrong'); + return; + } + + history.push('/login'); + }; + + const handleCreateContinueButton = async () => { + if (!usernameInput || !passwordInput) { + setInfo('One of more fields is not filled in.'); + return; + } + + const { json, isOK } = await APIRequest('/api/v1/users/account/create/info', { + method: 'GET', + headers: { + 'Accept': 'application/json', + } + }); + + if (!json || !isOK) return setInfo('Something went wrong'); + + setSpecialCodePrompt(json.requiresSpecialCode); + + if (!json.requiresSpecialCode) { + await doCreateAccount(); + } + } + + if (specialCodePrompt) return ( +
You need a special code to sign up here!
+ +