def signup(): an_error_has_ocurred = False username = request.form.get('username') password = request.form.get('password') repeat_password = request.form.get('password-repeat') errors = {} if not username or not utils.validate_data(username, 'username'): errors['username_error'] = 'Use alphanumeric characters (3 to 20) only' an_error_has_ocurred = True elif User.exist(username=username): errors['username_error'] = 'This username is already on use' an_error_has_ocurred = True if not password or not utils.validate_data(password, 'password'): errors['password_error'] = 'Use alphanumeric characters (3 to 20) only' an_error_has_ocurred = True elif not repeat_password or repeat_password != password: errors['repeat_password_error'] = 'The passwords do not match' an_error_has_ocurred = True if not an_error_has_ocurred: new_user = User(username=username, hashed_password=utils.encrypt_password(password)) new_user.save() response = make_response(redirect('/')) response.set_cookie('user-token', utils.gen_secure_cookie(new_user.id)) return response return render_template('index.html', username=username, **errors)
def login(): user = None an_error_has_ocurred = False username = request.form.get('username') password = request.form.get('password') errors = {} if not username or not utils.validate_data(username, 'username'): errors['username_error'] = 'Incorrect username' an_error_has_ocurred = True elif not User.exist(username=username): errors['username_error'] = 'This user does not exist' an_error_has_ocurred = True else: user = User.get(username=username) if not password: errors['password_error'] = 'Please, introduce the password' an_error_has_ocurred = True elif not utils.check_password(password, str(user.hashed_password)): errors['password_error'] = 'Wrong password' an_error_has_ocurred = True if not an_error_has_ocurred: response = make_response(redirect('/')) response.set_cookie('user-token', utils.gen_secure_cookie(user.id)) return response return render_template('index.html', username=username, **errors)