dimanche 24 février 2019

Preventing Endless Loop on Checkbox Input Conditional

I have a form submission process where I have middleware with if/else statements are validating that values are present before my authentication controller is triggered, but for some reason my Checkbox input field is triggering ERR_TOO_MANY_REDIRECTS on form submission and even when I try to load the form page after the submission. The reason for my undefined check is because I noticed that the body does not contain this input if the value isn't checked. Is there a better condition to use to check if this value is checked or not checked that will eliminate the redirect loop I am seeing? Is there a way to set this middleware to only trigger on the POST portion of the route?

Middleware that I am using for /sign-up:

siteRoutes.use('/sign-up', function(req, res, next){
    console.log("Sign Up Use")
    console.log(req.body);
    models.User.findOne({
        where: {
            email: req.body.email
        }
    }).then(function(existingUser) {
    if (existingUser){
        req.flash('error', 'Email already exists.');
        return res.redirect('/sign-up');
    } else if (req.body.termsOfService === undefined) {
        console.log("TOS Check")
        req.flash('error', 'Accept terms');
        return res.redirect('/sign-up');
    } else {
        console.log("Next")
        next();
    }
    });
});

Input Fields:

   <input type="text" class="form-control" id="sign-up-fist-name"  name="firstName" value="" placeholder="First Name">
                <br />
                    <input type="text" class="form-control" id="sign-up-last-name"  name="lastName" value="" placeholder="Last Name">
                <br />
                    <input type="text" class="form-control" id="sign-up-username"  name="email" value="" placeholder="Email Address">
                <br />
                    <input type="password" class="form-control" id="sign-up-password"  name="password" value="" placeholder="Password">
                <br />
                    <input type="password" class="form-control" id="sign-up-confirm-password"  name="confirmPassword" value="" placeholder="Confirm Password">
                <br />
                    <label>
                        <input type="checkbox" name="termsOfService"> I confirm that I have read, consent and agree to Synotate's <a href="/terms-of-service">Terms of Service</a> and <a href="privacy-policy">Privacy Policy</a>
                    </label>




Aucun commentaire:

Enregistrer un commentaire