Esempio n. 1
0
def auth_user():
    ''' auth endpoint '''
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        user = mongo.db.users.find_one({'email': data['email']}, {"_id": 0})
        LOG.debug(user)
        if user and flask_bcrypt.check_password_hash(user['password'],
                                                     data['password']):
            del user['password']
            access_token = create_access_token(identity=data)
            refresh_token = create_refresh_token(identity=data)
            user['token'] = access_token
            user['refresh'] = refresh_token
            return jsonify({'ok': True, 'data': user}), 200
        else:
            return jsonify({
                'ok': False,
                'message': 'invalid username or password'
            }), 401
    else:
        return jsonify({
            'ok':
            False,
            'message':
            'Bad request parameters: {}'.format(data['message'])
        }), 400
Esempio n. 2
0
def register():
    ''' register user endpoint '''
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        data['password'] = flask_bcrypt.generate_password_hash(
                            data['password'])
        user_id = mongo.db.user.insert_one(data).inserted_id
        closet_id = create_closet(user_id)
        wishlist_id = create_wishlist(user_id)
        mongo.db.user.find_one_and_update({"_id": user_id},
                                 {"$set": {"closet": closet_id, "wishlist": wishlist_id}})
        LOG.info('Registered user: {}'.format(user_id))
        return jsonify({'ok': True, 'message': 'User created successfully!'}), 200
    else:
        return jsonify({'ok': False, 'message': 'Bad request parameters: {}'.format(data['message'])}), 400
Esempio n. 3
0
def register():
    data = validate_user(request.get_json())
    if not data['ok']:
        return jsonify({'ok': False, 'message': 'Bad request parameters: {}'.format(data['message'])}), 400

    data = data['data']
    data['user'] = data['user'].upper()
    user = mongo.db.users.find_one({'user': data['user']})
    if user:
        return jsonify({'ok': False, 'message': 'El usuario {} ya existe'.format(data['user'])}), 400
    
    data['password'] = flask_bcrypt.generate_password_hash(data['password'])
    data['role'] = 'USER_ROLE'
    data['active'] = True
    mongo.db.users.insert_one(data)
    return jsonify({'ok': True, 'message': 'Usuario creado exitosamente!'}), 201
Esempio n. 4
0
def register():
    ''' register user endpoint '''
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        data['password'] = flask_bcrypt.generate_password_hash(
            data['password'])
        mongo.db.users.insert_one(data)
        return jsonify({
            'ok': True,
            'message': 'User created successfully!'
        }), 200
    else:
        return jsonify({
            'ok':
            False,
            'message':
            'Bad request parameters: {}'.format(data['message'])
        }), 400
Esempio n. 5
0
def auth_user():
    ''' auth endpoint '''
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        user = mongo.db.user.find_one({'email': data['email']})
        is_valid_user = user is not None
        is_valid_password = is_valid_user and flask_bcrypt.check_password_hash(user['password'], data['password'])
        if is_valid_user:
            if is_valid_password:
                del user['password']
                access_token = create_access_token(identity=data)
                refresh_token = create_refresh_token(identity=data)
                user['token'] = access_token
                user['refresh'] = refresh_token
                return jsonify({'ok': True, 'data': user}), 200
            else:
                return jsonify({'ok': False, 'message': 'Invalid password'}), 401
        else:
            return jsonify({'ok': False, 'message': 'Invalid email'}), 401

    else:
        return jsonify({'ok': False, 'message': 'Bad request parameters: {}'.format(data['message'])}), 400
Esempio n. 6
0
def auth_user():
    # auth endpoint
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        found_user = mongo.db.users.find_one({'email': data['email']})
        if found_user and flask_bcrypt.check_password_hash(
                found_user['password'], data['password']):
            del found_user['password']
            found_user['token'] = create_access_token(identity=data)
            found_user['refresh'] = create_refresh_token(identity=data)
            return jsonify({'ok': True, 'data': found_user}), 200
        else:
            return jsonify({
                'ok': False,
                'message': 'invalid username or password'
            }), 401
    else:
        return jsonify({
            'ok':
            False,
            'message':
            'Bad request parameters: {}'.format(data['message'])
        }), 400
Esempio n. 7
0
def auth_user():
    try:
        data = validate_user(request.get_json())
        if not data['ok']:
            return jsonify({'ok': False, 'message': 'Bad request parameters: {}'.format(data['message'])}), 400

        data = data['data']
        user = mongo.db.users.find_one({'user': data['user'].upper()}, {'_id': False, 'role': False})
        if not user or not flask_bcrypt.check_password_hash(user['password'], data['password']):
            return jsonify({'ok': False, 'message': 'Usuario o contraseña incorrectos'}), 401

        del user['password']
        del data['password']
        if not user['active']:
            return jsonify({'ok': False, 'message': 'Usuario o contraseña incorrectos'}), 401

        del user['active']
        access_token = create_access_token(identity=data)
        refresh_token = create_refresh_token(identity=data)
        user['token'] = access_token
        user['refresh'] = refresh_token
        return jsonify({'ok': True, 'data': user}), 200
    except:
        return jsonify({'ok': False, 'message': 'Usuario o contraseña incorrectos'}), 401