async def get_myself(self, request): user_info: UserClaim = get_bearer_token(self.jwt_secret, request) user = await self.user_repository.find_user_by_id(user_info.id) if not user: return json_response(reason=f"user not found", status=404) return json_response(result=user_model_to_dict(user))
async def update_myself(self, request): user_info: UserClaim = get_bearer_token(self.jwt_secret, request) request_body: UpdateUserRequest = convert_request( UpdateUserRequest, await request.json()) if not is_valid_email(request_body.email): return json_response( reason=f"{request_body.email} is invalid email format", status=400) user: User = await self.user_repository.find_user_by_id(user_info.id) if not user: return json_response(reason=f"user not found", status=404) verified_status = {} if user.email != request_body.email: verified_status["is_email_verified"] = False affected_rows = await self.user_repository.update_user( user_id=user_info.id, email=request_body.email, extra=request_body.extra, **verified_status, ) await self._send_user_update_event( original=user, delta={ "email": request_body.email, "extra": request_body.extra, "is_email_verified": False, }, ) return json_response(result=affected_rows > 0)
async def update_email_user_password(self, request): user_info: UserClaim = get_bearer_token(self.jwt_secret, request) request_body: UpdateUserPasswordRequest = convert_request( UpdateUserPasswordRequest, await request.json()) user: User = await self.user_repository.find_user_by_id(user_info.id) if not user: return json_response(reason=f"user not found", status=404) if user.type != UserType.EMAIL: return json_response(reason=f"only email user can update password", status=404) if not is_valid_password(request_body.new_password): return json_response(reason="password policy is not satisfied", status=400) if not bcrypt.checkpw(request_body.original_password.encode(), user.hashed_password.encode()): return json_response(reason="Invalid password", status=403) hashed_password = bcrypt.hashpw(request_body.new_password.encode(), bcrypt.gensalt()).decode() affected_rows = await self.user_repository.update_user( user_id=user_info.id, hashed_password=hashed_password, ) return json_response(result=affected_rows > 0)
async def update_myself(self, request): user_info: UserClaim = get_bearer_token(self.jwt_secret, request) request_body: UpdateUserRequest = convert_request( UpdateUserRequest, await request.json()) if not is_valid_email(request_body.email): return json_response( reason=f'{request_body.email} is invalid email format', status=400) user: User = await find_user_by_id(user_info.id) if not user: return json_response(reason=f'user not found', status=404) verified_status = {} if user.email != request_body.email: verified_status['is_email_verified'] = False affected_rows = await update_user( user_id=user_info.id, email=request_body.email, extra=request_body.extra, **verified_status, ) return json_response(result=affected_rows > 0)
async def get(self, request): user_info: UserClaim = get_bearer_token(self.jwt_secret, request) return json_response(result=object_to_dict(user_info))