Skip to content

๐Ÿคš โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป

โฎ๏ธ ๐Ÿ“ƒ ๐Ÿ’‚โ€โ™‚ โš™๏ธ (โ” ๐Ÿงข ๐Ÿ”› ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ) ๐Ÿค โžก ๐Ÿ› ๏ธ ๐Ÿ”ข token str:

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
    return {"token": token}

โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ‘ˆ โš .

โžก๏ธ โš’ โšซ๏ธ ๐Ÿค ๐Ÿ‘ฅ โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป.

โœ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿท

๐Ÿฅ‡, โžก๏ธ โœ Pydantic ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿท.

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘ฅ โš™๏ธ Pydantic ๐Ÿ“ฃ ๐Ÿ’ช, ๐Ÿ‘ฅ ๐Ÿ’ช โš™๏ธ โšซ๏ธ ๐Ÿ™† ๐Ÿ™†:

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: str | None = None
    full_name: str | None = None
    disabled: bool | None = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user

โœ get_current_user ๐Ÿ”—

โžก๏ธ โœ ๐Ÿ”— get_current_user.

๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ”— ๐Ÿ’ช โœ”๏ธ ๐ŸŽง-๐Ÿ”— โ“

get_current_user ๐Ÿ”œ โœ”๏ธ ๐Ÿ”— โฎ๏ธ ๐ŸŽ oauth2_scheme ๐Ÿ‘ฅ โœ โญ.

๐ŸŽ ๐Ÿ‘ฅ ๐Ÿ”จ โญ โžก ๐Ÿ› ๏ธ ๐Ÿ”—, ๐Ÿ‘† ๐Ÿ†• ๐Ÿ”— get_current_user ๐Ÿ”œ ๐Ÿ“จ token str โšช๏ธโžก๏ธ ๐ŸŽง-๐Ÿ”— oauth2_scheme:

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: str | None = None
    full_name: str | None = None
    disabled: bool | None = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user

๐Ÿคš ๐Ÿ‘ฉโ€๐Ÿ’ป

get_current_user ๐Ÿ”œ โš™๏ธ (โŒ) ๐Ÿš™ ๐Ÿ”ข ๐Ÿ‘ฅ โœ, ๐Ÿ‘ˆ โœŠ ๐Ÿค str & ๐Ÿ“จ ๐Ÿ‘† Pydantic User ๐Ÿท:

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: str | None = None
    full_name: str | None = None
    disabled: bool | None = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user

๐Ÿ’‰ โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป

๐Ÿ”œ ๐Ÿ‘ฅ ๐Ÿ’ช โš™๏ธ ๐ŸŽ Depends โฎ๏ธ ๐Ÿ‘† get_current_user โžก ๐Ÿ› ๏ธ:

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: str | None = None
    full_name: str | None = None
    disabled: bool | None = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ“ฃ ๐Ÿ†Ž current_user Pydantic ๐Ÿท User.

๐Ÿ‘‰ ๐Ÿ”œ โ„น ๐Ÿ‡บ๐Ÿ‡ฒ ๐Ÿ”˜ ๐Ÿ”ข โฎ๏ธ ๐ŸŒ ๐Ÿ› ๏ธ & ๐Ÿ†Ž โœ….

Tip

๐Ÿ‘† 5๏ธโƒฃ๐Ÿ“† ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿ’ช ๐Ÿ“ฃ โฎ๏ธ Pydantic ๐Ÿท.

๐Ÿ“ฅ FastAPI ๐Ÿ† ๐Ÿšซ ๐Ÿคš ๐Ÿ˜จ โ†ฉ๏ธ ๐Ÿ‘† โš™๏ธ Depends.

Check

๐ŸŒŒ ๐Ÿ‘‰ ๐Ÿ”— โš™๏ธ ๐Ÿ— โœ” ๐Ÿ‘ฅ โœ”๏ธ ๐ŸŽ ๐Ÿ”— (๐ŸŽ "โ˜‘") ๐Ÿ‘ˆ ๐ŸŒ ๐Ÿ“จ User ๐Ÿท.

๐Ÿ‘ฅ ๐Ÿšซ ๐Ÿšซ โœ”๏ธ ๐Ÿ•ด 1๏ธโƒฃ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿ†Ž ๐Ÿ’ฝ.

๐ŸŽ ๐Ÿท

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”œ ๐Ÿคš โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”— โžก ๐Ÿ› ๏ธ ๐Ÿ”ข & ๐Ÿ™… โฎ๏ธ ๐Ÿ’‚โ€โ™‚ ๐Ÿ› ๏ธ ๐Ÿ”— ๐Ÿ’‰ ๐ŸŽš, โš™๏ธ Depends.

& ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ™† ๐Ÿท โš–๏ธ ๐Ÿ’ฝ ๐Ÿ’‚โ€โ™‚ ๐Ÿ“„ (๐Ÿ‘‰ ๐Ÿ’ผ, Pydantic ๐Ÿท User).

โœ‹๏ธ ๐Ÿ‘† ๐Ÿšซ ๐Ÿšซ โš™๏ธ ๐ŸŽฏ ๐Ÿ’ฝ ๐Ÿท, ๐ŸŽ“ โš–๏ธ ๐Ÿ†Ž.

๐Ÿ‘† ๐Ÿ’š โœ”๏ธ id & email & ๐Ÿšซ โœ”๏ธ ๐Ÿ™† username ๐Ÿ‘† ๐Ÿท โ“ ๐Ÿ’ญ. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ‘‰ ๐ŸŽ ๐Ÿงฐ.

๐Ÿ‘† ๐Ÿ’š โœ”๏ธ strโ“ โš–๏ธ dictโ“ โš–๏ธ ๐Ÿ’ฝ ๐ŸŽ“ ๐Ÿท ๐Ÿ‘ ๐Ÿ”— โ“ โšซ๏ธ ๐ŸŒ ๐Ÿ‘ท ๐ŸŽ ๐ŸŒŒ.

๐Ÿ‘† ๐Ÿค™ ๐Ÿšซ โœ”๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ ๐Ÿ•น ๐Ÿ‘† ๐Ÿˆธ โœ‹๏ธ ๐Ÿค–, ๐Ÿค–, โš–๏ธ ๐ŸŽ โš™๏ธ, ๐Ÿ‘ˆ โœ”๏ธ ๐Ÿ” ๐Ÿค โ“ ๐Ÿ”„, โšซ๏ธ ๐ŸŒ ๐Ÿ‘ท ๐ŸŽ.

โš™๏ธ ๐Ÿ™† ๐Ÿ˜‡ ๐Ÿท, ๐Ÿ™† ๐Ÿ˜‡ ๐ŸŽ“, ๐Ÿ™† ๐Ÿ˜‡ ๐Ÿ’ฝ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘† ๐Ÿˆธ. FastAPI โœ”๏ธ ๐Ÿ‘† ๐Ÿ“” โฎ๏ธ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ.

๐Ÿ“Ÿ ๐Ÿ“

๐Ÿ‘‰ ๐Ÿ–ผ 5๏ธโƒฃ๐Ÿ“† ๐Ÿ˜‘ ๐Ÿ”. โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐ŸŒ€ ๐Ÿ’‚โ€โ™‚, ๐Ÿ“Š ๐Ÿท, ๐Ÿš™ ๐Ÿ”ข & โžก ๐Ÿ› ๏ธ ๐ŸŽ ๐Ÿ“.

โœ‹๏ธ ๐Ÿ“ฅ ๐Ÿ”‘ โ˜.

๐Ÿ’‚โ€โ™‚ & ๐Ÿ”— ๐Ÿ’‰ ๐Ÿ’ฉ โœ ๐Ÿ•.

& ๐Ÿ‘† ๐Ÿ’ช โš’ โšซ๏ธ ๐Ÿ— ๐Ÿ‘† ๐Ÿ’š. & , โœ”๏ธ โšซ๏ธ โœ ๐Ÿ•ด ๐Ÿ•, ๐Ÿ‘ ๐Ÿฅ‰. โฎ๏ธ ๐ŸŒ ๐Ÿ’ช.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ ๐Ÿ’ฏ ๐Ÿ”— (โžก ๐Ÿ› ๏ธ) โš™๏ธ ๐ŸŽ ๐Ÿ’‚โ€โ™‚ โš™๏ธ.

& ๐ŸŒ ๐Ÿ‘ซ (โš–๏ธ ๐Ÿ™† โ†” ๐Ÿ‘ซ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š) ๐Ÿ’ช โœŠ ๐Ÿ“ˆ ๐Ÿค-โš™๏ธ ๐Ÿ‘ซ ๐Ÿ”— โš–๏ธ ๐Ÿ™† ๐ŸŽ ๐Ÿ”— ๐Ÿ‘† โœ.

& ๐ŸŒ ๐Ÿ‘‰ ๐Ÿ’ฏ โžก ๐Ÿ› ๏ธ ๐Ÿ’ช ๐Ÿคช 3๏ธโƒฃ โธ:

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: str | None = None
    full_name: str | None = None
    disabled: bool | None = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user

๐ŸŒƒ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”œ ๐Ÿคš โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”— ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

๐Ÿ‘ฅ โช ๐Ÿ˜ฌ ๐Ÿ“ค.

๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ โžก ๐Ÿ› ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป/๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿค™ ๐Ÿ“จ username & password.

๐Ÿ‘ˆ ๐Ÿ‘Ÿ โญ.