yezz123 / authx

Ready-to-use and customizable Authentications and Oauth2 management for FastAPI ✨
https://authx.yezz.me/
MIT License
796 stars 48 forks source link

MongoDBBackend has no attribute client #254

Closed YogeshUpdhyay closed 2 years ago

YogeshUpdhyay commented 2 years ago

First Check

Example Code

from authx import Authentication, MongoDBBackend
import motor.motor_asyncio
import asyncio

auth = Authentication(
    backend=MongoDBBackend(
        client=motor.motor_asyncio.AsyncIOMotorClient(
            'mongodb://localhost:27017',
            io_loop=asyncio.get_event_loop()
        ),
        database='authx',
        collection='users'
    )
)

Description

This should ideally create an auth object that can be used to include routers. Instead this gives an error

    backend=MongoDBBackend(
TypeError: __init__() got an unexpected keyword argument 'client'

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.77.1

Python Version

Python 3.9.0

Additional Context

This problem is arising as the MongoDBBackend class is not excepting any other parameters other than the database_name

class MongoDBBackend(BaseDBBackend):
    """
    Setup Database for authx using MongoDB & Motor
    """

    def __init__(self, database_name: str = "test") -> None:
        self._database_name = database_name

    def set_client(self, client: AsyncIOMotorClient) -> None:
        self._client = client
        self.init()

    def init(self) -> None:
        self._db: AsyncIOMotorDatabase = self._client[self._database_name]
        self._users: AsyncIOMotorCollection = self._db["users"]
        self._email_confirmations: AsyncIOMotorCollection = self._db[
            "email_confirmations"
        ]
        self._counters: AsyncIOMotorCollection = self._db["counters"]

        self._settings: AsyncIOMotorCollection = self._db["settings"]
yezz123 commented 2 years ago

hello @YogeshUpdhyay can you please check the PR I mention #260