Exemplo n.º 1
0
def auth_user():
    """ auth a.k.a login endpoint """
    data = validate_user(request.get_json())
    if data['ok']:
        data = data['data']
        user_record = mongo.db.users.find_one({'email': data['email']},
                                              {"_id": 0})
        if user_record and flask_bcrypt.check_password_hash(
                user_record['password'], data['password']):
            del user_record['password']
            access_token = create_access_token(identity=data)
            refresh_token = create_refresh_token(identity=data)
            user_record['token'] = access_token
            user_record['refresh'] = refresh_token
            return jsonify({'ok': True, 'data': user_record}), 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
Exemplo n.º 2
0
 def test_invalid_user(self):
     data = {
         "_id": "123456",
         "email": "jdoe@@example.com",
         "phone_numbers": ["123-456-7890"],
         "role": "research coordinator"
     }
     res = validate_user(data)
     self.assertFalse(res['ok'])
Exemplo n.º 3
0
 def test_valid_user(self):
     data = {
         "_id": "123456",
         "first_name": "John",
         "last_name": "Doe",
         "email": "*****@*****.**",
         "phone_numbers": ["123-456-7890"],
         "role": "research coordinator"
     }
     res = validate_user(data)
     self.assertTrue(res['ok'])
Exemplo 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
Exemplo n.º 5
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']})
        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': user, 'refresh': refresh_token, 'token': access_token}]}), 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