IndominusByte / fastapi-jwt-auth

FastAPI extension that provides JWT Auth support (secure, easy to use, and lightweight)
http://indominusbyte.github.io/fastapi-jwt-auth/
MIT License
660 stars 153 forks source link

AttributeError: 'Depends' object has no attribute 'create_access_token' #55

Open dasunprem opened 3 years ago

dasunprem commented 3 years ago

Hi,

I'm receiving the error : AttributeError: 'Depends' object has no attribute 'create_access_token'

my code is as follows: def Authenticate_user(request: authSchema.loginSchema, Authorize: AuthJWT=Depends()): try: user = db.users.find_one({"email": request.email}) print(user["_id"]) print(request.password) if user == None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=errors.REGISTER_INSTEAD) if not Hash.verify(user["passhash"], request.password): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=errors.AUTH_ERROR) access_token = Authorize.create_access_token(subject=user["_id"]) print(access_token) refresh_token = Authorize.create_refresh_token(subject=user.username) print(refresh_token) return { "token_type": "bearer", "access_token": access_token, "refresh_token":refresh_token, "user": { "id": user.id, "f_name": user.f_name, "l_name": user.l_name, "email": user.email, "mobile": user.mobile } } except HTTPException: return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=errors.SERVER_ERROR) except ServerSelectionTimeoutError: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=errors.DB_ERROR)

SamAlhabash commented 3 years ago

Can you please post your entire file with your imports, along with your OS and python versions so we can have a better look at this problem? I think it is probably a problem with your imports where you are not importing the right AuthJWT.

SamAlhabash commented 3 years ago

Can you please post your entire file with your imports, along with your OS and python versions so we can have a better look at this problem? I think it is probably a problem with your imports where you are not importing the right AuthJWT.

dasunprem commented 3 years ago

@SamAlhabash I just got this fixed few minutes ago.

This was my error: my folder/file structure is as follows. AuthAPI |- lib/ |- auth.py |- routes/ |- auth.py |- schemas/ |- main.py

the code I posted on the question is on the lib/auth.py. my route was not having the Authorize : AuthJWT = Depends().

now my route looks like the following: @router.post('/login') def login(request: authSchema.loginSchema, Authorize: AuthJWT=Depends()): return auth.Authenticate_user(request, Authorize)