Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can OAuth login via Json data instead of Form? #1431

Closed
LouisYZK opened this issue May 19, 2020 · 5 comments
Closed

Can OAuth login via Json data instead of Form? #1431

LouisYZK opened this issue May 19, 2020 · 5 comments
Labels
question Question or problem question-migrate

Comments

@LouisYZK
Copy link

Description

I want to use the jwt auth, and learn the tutorial about it. There are sth confused me:

@router.post('/token')
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user = await authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(status_code=400, detail='incorrect username')
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = await create_access_token(data={'sub': user.username}, expires_delta=access_token_expires)
    return {'access_token': access_token, 'token_type': 'bearer'}

If login success, the token will be returned , and next time you send other request need auth like

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/token')
async def get_current_user(token: str=Depends(oauth2_scheme)):
    pass
  • How does it works the next request was added a header with Authorization: Bear automically? Where does the token returned stored?
  • When I replace the Form with json, the following requests after login are failing to be with Auth Header
async def login(user_data: schemas.UserLogin):
    pass

I am sorry about my English and less experience about auth in Web, wish you could solve my confusion. Thanks in advance.

@LouisYZK LouisYZK added the question Question or problem label May 19, 2020
@dmig-alarstudios
Copy link

Look inside the OAuth2PasswordRequestForm -- form fields are described there. You can replace this class with your own, accepting application/json instead of application/x-www-form-urlencoded.

@tiangolo
Copy link
Owner

tiangolo commented Jun 6, 2020

Thanks for the help here @dmig-alarstudios ! 👏 🙇

Thanks for reporting back and closing the issue @LouisYZK 👍

@bartwroblewski
Copy link

If one makes a custom class as per @dmig-alarstudios suggestion, is it possible to also (auto-)update the OAuth Swagger form to use application/json instead of www-form-urlencoded?

@dmig-alarstudios
Copy link

@bartwroblewski I couldn't find a solution for that ended up with a duplicate endpoint accepting www-form-urlencoded

@yashp280793
Copy link

yashp280793 commented Dec 14, 2022

@dmig-alarstudios I have a requirement where frontend is posting content using content-type = "application/json". Due to OAuth2PasswordRequestForm,I am getting error as "username field required".

Is there a way to add support of application/json or any middleware to convert incoming json to www-form-urlencoded?
I am in situation that either frontend works (with custom class) or swagger works with OAuth2PasswordRequestForm.

@tiangolo tiangolo changed the title [QUESTION] Can OAuth login via Json data instead of Form? Can OAuth login via Json data instead of Form? Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7616 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

5 participants