示例#1
0
def register():
    if request.method == 'POST':
        username = request.json.get('username')
        password = request.json.get('password')
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif User.select().where(User.username == username).exists():
            error = 'Username exists in database'

        if error is None:
            instance = User.create(username=username,
                                   password=generate_password_hash(password))
            return redirect(url_for('auth.login'))
        else:
            return jsonify({'error': error})

    return redirect(url_for('index'))