Exemplo n.º 1
0
    def login_user(data, ip):
        print('~~~ [login_user] ip', ip)
        try:
            login_attempt = LoginAttempts.query.filter_by(ip=ip).first()
            if login_attempt is not None:
                # print('\t ******* login_attempt', login_attempt, login_attempt.failed_login_attempts)
                time_delt = delta_time(datetime.datetime.now(),
                                       login_attempt.failed_login_time)
                if login_attempt.failed_login_attempts == 3 and time_delt < 3:
                    print('\t ******* ERROR', login_attempt,
                          login_attempt.failed_login_attempts)
                    return error(
                        message=
                        'You\'ve reached limit tries. Please try again in {} minutes.'
                        .format(round(3 - time_delt, 2)))

            # user = User.query.filter_by(email=cf.sanitize_data(data.get('email'))).first()
            user = User.query.filter_by(email=data.get('email')).first()
            print("data.get('password')", data.get('password'))
            if user and user.check_password(data.get('password')):
                auth_token = User.encode_auth_token(user.user_id)
                if user.blocked:
                    return error(message='User has been blocked')
                if auth_token:
                    return result(message='Successfully logged in',
                                  data={'Authorization': auth_token.decode()})
            else:
                # ControllerUser.update()
                if login_attempt is None:
                    # insert login_attempt of this ip
                    ip_login_attempt = LoginAttempts(ip=ip)
                    db.session.add(ip_login_attempt)
                    db.session.commit()
                else:
                    if delta_time(datetime.datetime.now(),
                                  login_attempt.failed_login_time) > 3:
                        login_attempt.failed_login_attempts = 1
                    else:
                        login_attempt.failed_login_attempts += 1
                        # print('\t ****** modify! login_attempt.failed_login_attempts', login_attempt.failed_login_attempts)
                    login_attempt.failed_login_time = datetime.datetime.now()
                    db.session.commit()
                return error(message='Email or Password does not match')
        except Exception as e:
            return error(message=e)
 def login_user(data):
     try:
         user = User.query.filter_by(email=data.get('email')).first()
         if user and user.check_password(data.get('password')):
             auth_token = User.encode_auth_token(user.user_id)
             if user.blocked:
                 return None  # error(message='User has been blocked')
             if auth_token:
                 role = user.role
                 if role.__eq__('user'):
                     pass
                 if role.__eq__('buyer'):
                     buyer_id = get_id(user_id=user.user_id, role=role)
                     print("I am here", buyer_id)
                     user.buyer_id = buyer_id
                 if role.__eq__('supplier'):
                     supplier_id = get_id(user_id=user.user_id, role=role)
                     user.supplier_id = supplier_id
                 return user  # result(message='Successfully logged in', data={'Authorization': auth_token.decode()})
         else:
             return None  # error(message='Email or Password does not match')
     except Exception as e:
         return error(message=e)