[Python] How do I set JWT refresh token location to cookies without having...

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating a fastapi backend for user authentication with jwt. I am using fastapi-jwt-auth library to create access and refresh tokens.

    I am trying to send the refresh token in a cookie.

    @router.post('/login')
    def login_user(user: UserRequest, db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
    db_user = db.query(UserModel).filter(UserModel.email == user.email).first()
    if not db_user or not verify_pwd(user.pwd, db_user.hashed_pwd):
    raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid Credentials')

    access_token = Authorize.create_access_token(subject=db_user.email)
    refresh_token = Authorize.create_refresh_token(subject=db_user.email)
    Authorize.set_refresh_cookies(refresh_token) # to send refresh token in cookies
    return {
    'access_token': access_token,
    }


    but was getting an error which said that I need to set the authjwt_token_location.

    I added authjwt_token_location in the Settings class I made:

    class Settings(BaseSettings):
    authjwt_secret_key: str = os.environ.get('JWT_SECRET_KEY','YOUR_SECRET_KEY')
    authjwt_token_location: Optional[Sequence[StrictStr]] = {'cookies'}

    @AuthJWT.load_config
    def get_config():
    return Settings()


    I have also tried using:

    authjwt_token_location: set = {'cookies'} # still gives error


    After setting the authjwt_token_location I keep getting a pydantic_core Validation error:

    pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
    authjwt_token_location
    Input should be an instance of Sequence [type=is_instance_of, input_value={'cookies'}, input_type=set]


    I also tried using different types of Sequences e.g list, tuple, str etc, but get the this error:

    pydantic_core._pydantic_core.ValidationError: 1 validation error for LoadConfig
    authjwt_token_location
    Value error, The 'authjwt_token_location' must be between 'headers' or 'cookies' [type=value_error, input_value=['cookies'], input_type=list]


    I have searched extensively but couldn't find anything. If anyone else have faced these errors, kindly guide me.

    I am using versions: python 3.13.6

    pydantic: 2.11.7

    fastapi: 0.116.1

    fastapi-jwt-auth: 0.5.0

    Continue reading...

Compartilhe esta Página