def logout_user(data): if data: auth_token = data.split(" ")[1] else: auth_token = '' if auth_token: resp = User.decode_auth_token(auth_token) if not isinstance(resp, str): return save_token(token=auth_token) else: return error(message=resp) else: return error(message='Provide a valid auth token')
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 get_logged_user(new_request): auth_token = new_request.headers.get('Authorization') if auth_token: auth_token = auth_token.split(' ')[1] resp = User.decode_auth_token(auth_token) if not isinstance(resp, str): user = User.query.filter_by(user_id=resp).first() print(user) res = { 'user_id': user.user_id, 'email': user.email, 'role': user.role, 'name': user.name } return result(data=res) return error(message=resp) else: return error(message='Provide a valid auth token')
def get_logged_user(new_request): auth_token = new_request.headers.get('Authorization') if auth_token: auth_token = auth_token.split(' ')[1] resp = User.decode_auth_token(auth_token) if not isinstance(resp, str): user = User.query.filter_by(user_id=resp).first() return user # tra lai JSON tương ứng về các roles đang thực hiện và các orders. # # print(user) # res = { # 'user_id': user.user_id, # 'email': user.email, # 'role': user.role, # 'name': user.name # } # return result(data=res) return None # error(message=resp) else: return None # error(message='Provide a valid auth token')
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)
def _parse_user(self, data, user=None): name, surname, middlename, fullname, age, birthday, home_address, home_country, home_city, home_street, home_geo_long, home_geo_lat, phone, email, username, passwordHash, blocked, token, facebook, instagram, vkontakte, avatar, isadmin = None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None if 'name' in data: name = data['name'] if 'surname' in data: surname = data['surname'] if 'middlename' in data: middlename = data['middlename'] if 'fullname' in data: fullname = data['fullname'] if 'age' in data: age = int(data['age']) if 'birthday' in data: try: birthday = date.fromisoformat(data['birthday']) except Exception as e: print(e.__str__()) pass if 'home_address' in data: home_address = data['home_address'] if 'home_country' in data: home_country = data['home_country'] if 'home_city' in data: home_city = data['home_city'] if 'home_street' in data: home_street = data['home_street'] if 'home_geo_long' in data: home_geo_long = data['home_geo_long'] if 'home_geo_lat' in data: home_geo_lat = data['home_geo_lat'] if 'phone' in data: phone = data['phone'] # email bat buoc phai co email = data['email'] if 'username' in data: username = data['username'] # password bat buoc phai co password = data['password'] passwordHash = flask_bcrypt.generate_password_hash(password) if 'blocked' in data: blocked = bool(data['blocked']) if 'token' in data: token = data['token'] if 'facebook' in data: facebook = data['facebook'] if 'instagram' in data: instagram = data['instagram'] if 'vkontakte' in data: vkontakte = data['vkontakte'] if 'avatar' in data: avatar = data['avatar'] if 'isadmin' in data: isadmin = bool(data['isadmin']) if user is None: user = User(name=name, surname=surname, middlename=middlename, fullname=fullname, age=age, birthday=birthday, home_address=home_address, home_country=home_country, home_city=home_city, home_street=home_street, home_geo_long=home_geo_long, home_geo_lat=home_geo_lat, phone=phone, email=email, username=username, password_hash=passwordHash, blocked=blocked, token=token, facebook=facebook, instagram=instagram, vkontakte=vkontakte, avatar=avatar, isadmin=isadmin) else: user.name = name user.surname = surname user.middlename = middlename user.fullname = fullname user.age = age user.birthday = birthday user.home_address = home_address user.home_country = home_country user.home_city = home_city user.home_street = home_street user.home_geo_long = home_geo_long user.home_geo_lat = home_geo_lat user.phone = phone user.email = email user.username = username user.password_hash = passwordHash user.blocked = blocked user.token = token user.facebook = facebook user.instagram = instagram user.vkontakte = vkontakte user.avatar = avatar user.isadmin = isadmin return user