diff --git a/packages/server/play-node-go/src/components/Signup/Signup.js b/packages/server/play-node-go/src/components/Signup/Signup.js index b8bae10..b454da0 100644 --- a/packages/server/play-node-go/src/components/Signup/Signup.js +++ b/packages/server/play-node-go/src/components/Signup/Signup.js @@ -8,30 +8,73 @@ const Signup = (props) => { const [ email, setEmail ] = useState(''); const [ password, setPassword ] = useState(''); const [ confirmPassword, setConfirmPassword ] = useState(''); + const minimumPasswordLength = 8; + const errorDispatchAction = { + type: 'ERR', + message: 'AUTH_ERROR' + } - const handleSubmit = async e => { - e.preventDefault(); + const validateSignupForm = (next) => { if (password !== confirmPassword) { return props.dispatch({ - type: 'ERR', - message: 'AUTH_ERROR', + ...errorDispatchAction, body: { authError: 'Password fields must be the same'} }) } + if (password.length < 8) { + return props.dispatch({ + ...errorDispatchAction, + body: { authError: 'Password must be at least 8 characters'} + }) + } + + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { + return props.dispatch({ + ...errorDispatchAction, + body: { authError: 'Please enter a valid email'} + }) + } + + if (!/^[\w\d_.-]+$/.test(username)) { + return props.dispatch({ + ...errorDispatchAction, + body: { authError: "Username can only contain letters, numbers, '_', '.', or '-'"} + }) + } + + return next(); + } + + const postSignupForm = async () => { const signupResponse = await authServices.signupService({ username, email, password, confirmPassword }) + const parsedResponse = JSON.parse(signupResponse) + + if (parsedResponse.errors) { + const authError = parsedResponse.errors[0].auth + return props.dispatch({ + ...errorDispatchAction, + body: { authError } + }) + } + return props.dispatch({ type: 'AUTH', message: 'SIGNUP', - body: signupResponse + body: parsedResponse }) } + const handleSubmit = async e => { + e.preventDefault(); + validateSignupForm(postSignupForm); + } + const formError = errors => { if(!errors) return <>; diff --git a/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js b/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js index 41e1aba..4ba18b6 100644 --- a/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js +++ b/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js @@ -25,25 +25,6 @@ function loginReducer(state: state, action: action): state { } function signupReducer(state: state, action: action): state { - const signupResponse = action.body; - let error; - - if (signupResponse.response) { - error = signupResponse.response.data.errors; - } - let responseUser; - if (signupResponse.data) { - responseUser = {...signupResponse.data} - } - - if (error) { - const errors = error.reduce((errorObject, error) => errorObject[Object.keys(error)[0] = error]) - return {...state, errors }; - } - - if (responseUser) { - return {...state, user: responseUser }; - } - - return {...state, errors: {requestError: 'something went wrong'}}; + const newUser = action.body; + return {...state, user: newUser }; } \ No newline at end of file diff --git a/packages/server/play-node-go/src/services/authServices.js b/packages/server/play-node-go/src/services/authServices.js index 96a76d6..d4318fd 100644 --- a/packages/server/play-node-go/src/services/authServices.js +++ b/packages/server/play-node-go/src/services/authServices.js @@ -21,7 +21,7 @@ const signupService = async (formData) => { headers: headers }) .then(res => { - return res; + return res.text(); }).catch(err => { return err; });