I'm following this tutorial to create an api backend.
I use firebase authentication:
- user input email and password at frontend
- front sends the info to firebase
- firebase auth user and return token
- front stores the token
- for any url that needs auth, front sends the token in
Authorizationheader (Bearer xxx) - server side firebase checks the token
The tutorial shows how to do this with a password:
# creating a dependency
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = fake_decode_token(token)
return user
But since I'm using firebase, there is no /token for getting token with password.
I can parse the token by creating a custom dependency, like:
async def parse_token(auth_token:str = Header(...)):
token = auth_token.split(' ')[1]
return token
async def get_current_user(token: str = Depends(parse_token)):
# check the token with firebase auth
user = auth.verify_id_token(token)
return user
But now I have to check everything and return exceptions manually.
Is there a FastAPI way to do this?