def add_claims_to_access_token(identity): urole = AuthModel.find_by_id(identity) print("urole = ", urole.role) if urole.role == 'Admin': return {'role': 'Admin'} elif urole.role == 'Vendor': return {'role': 'Vendor'} else: return {'role': 'Customer'}
def get(self): users = AuthModel.find_by_id(get_jwt_identity()) if users: jti = get_jwt()['jti'] blocklist.add(jti) return { "message": "User Logged out Successfully", "Status": "Success" } return {"Status": "Fail"}, 200
def put(self, id): parse = reqparse.RequestParser() parse.add_argument('password', type=str) data = parse.parse_args() user = AuthModel.find_by_id(id) if user: data = parse.parse_args() user.password = data['password'] user.save_to_db() return {"updated": True, "data": user.json()}, 200
def delete(self): parse = reqparse.RequestParser() parse.add_argument('username', type=str, required=True, help="Username is required") data = parse.parse_args() user = AuthModel.find_by_username(data['username']) print("USER = "******"message": "User DELETED Successfully"}, 200 return {"message": "No data"}, 400
def put(self, id): parse = reqparse.RequestParser() parse.add_argument('username', type=str) parse.add_argument('city', type=str) parse.add_argument('phone', type=str) data = parse.parse_args() user = AuthModel.find_by_id(id) if user: data = parse.parse_args() user.username = data['username'] user.city = data['city'] user.phone = data['phone'] user.save_to_db() return {"updated": True, "data": user.json()}, 200
def post(self): data = UserRegistration.parse.parse_args() user = AuthModel.find_by_username(data['username']) if user: return { "AlreadyExistError": { "error": "Username already exist" } }, 400 user = AuthModel(**data) user.save_to_db() return { "message": "User Created Successfully", "User": user.json() }, 201
def post(self): data = Registration.parse.parse_args() user = AuthModel.find_by_role_and_city("Vendor", data['city']) if user: return { "AlreadyExistError": { "error": "Vendor from this city already exist" } }, 400 user = AuthModel(**data) user.save_to_db() return { "message": "Vendor Created Successfully", "User": user.json() }, 201
def post(self): data = UserLogin.parse.parse_args() user = AuthModel.find_by_username(data['username']) if user and safe_str_cmp(user.password, data['password']): expires = timedelta(days=5) access_token = create_access_token(identity=user.id, expires_delta=expires, fresh=True) if user.role == "Customer": return { "username": user.username, "phone": user.phone, "city": user.city, "role": user.role, "access_token": access_token, }, 200 else: return { "role": user.role, "id": user.id, "access_token": access_token, }, 200 return {"error": "Username or Password may incorrect"}, 400
def get(self): user = AuthModel.find_by_id(get_jwt_identity()) if user: return {"User": user.json(), "status": 'ok'}, 200 return {"message": "User Not Found"}