async def find(self, user_id: UserId) -> User: await self._ensure_indexes() user: Optional[User] = await self._find_one(self._collection, {'id': user_id.value()}) if not user: raise UserNotFoundError.create(detail={'id': user_id.value()}) return user
async def notify_user_password_resetted(self, user_id: UserId, email: UserEmail) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Project - The password was reset', mime_text_plain_content='The password was reset', )
async def notify_user_registered(self, user_id: UserId, email: UserEmail) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Welcome to Project', mime_text_plain_content=f'Welcome {email.value()}', )
async def notify_user_password_forgotten( self, user_id: UserId, email: UserEmail, refresh_token: UserRefreshToken) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Project - Here you have your reset password code', mime_text_plain_content= f'Here you have your reset password code: {refresh_token.value()}', )
async def generate(self, user_id: UserId, expiration_in_days: int) -> str: now = self._now() return self._jwt.encode( header={'alg': 'HS256'}, payload={ 'iss': 'Authlib', 'sub': dumps({'id': user_id.value()}).decode('utf-8'), 'iat': now, 'exp': now + timedelta(days=expiration_in_days), }, key=self._secret, ).decode('utf-8')
async def search(self, user_id: UserId) -> Optional[User]: await self._ensure_indexes() return await self._find_one(self._collection, {'id': user_id.value()})
async def __call__(self, user_id: UserId, email: UserEmail, password: UserPassword) -> None: if await self._repository.search(user_id): raise UserAlreadyExistError.create(detail={'id': user_id.value()}) user = User.register(user_id, email, password) await self._repository.save_and_publish(user)