Example #1
0
def signup():
    if request.method == 'POST':
        username = request.form["username"]
        password = request.form["passwd"]
        confirm_password = request.form["confirmPasswd"]

        validate = Validate()
        validate.set_username_validators(min_length=4, max_length=12)

        username_check = validate.validate_username(username.strip())
        password_check = validate.validate_password(password, confirm_password)

        if username_check == 'PASS' and password_check == 'PASS':
            # Insert into database 
            global conn
            params = (username, password)
            conn.execute("INSERT INTO user (username, password) VALUES (?, ?)", params)
            conn.commit()

            session.permanent = True
            session['user'] = username
            return redirect(url_for('login', user=username))
        elif username_check == 'USERNAME_NULL':
            message = "username field is left blank"
        elif username_check == 'USERNAME_LENGTH_VIOLATED':
            message = "username length should be of min 4 and max 12 characters"
        elif username_check == 'USERNAME_VIOLATED':
            message = "username can consist of alphanumeric characters and cannot start with a digit"
        elif password_check == 'PASSWD_UNMATCH':
            message = "passwords do not match"
        elif password_check == 'PASSWD_WEAK':
            message = "weak password"
        else: 
            message = "some error occurred"

        return render_template('register.html', message=message)

    else:
        if user_authenticated(): 
            return render_template('force_logout.html')

    return render_template('register.html', message='')