Endpoints

If using Piccolo as a backend, there is an endpoint for logging in, which will return a token.


token_login

This creates an endpoint for logging in, and getting a token.

from piccolo_api.token_auth.endpoints import token_login
from starlette import Starlette
from starlette.routing import Route, Router


app = Starlette(
    routes=[
        Route("/login/", token_login()),
    ]
)

For the user to login you have to create a token for the user in the TokenAuth table (the easiest way is to create a token in Piccolo Admin).

Usage

You can use any HTTP client to get the token. In our example we use curl.

To get a token:

curl -X POST \
    -H "Content-Type: application/json"
    -d '{"username": "piccolo", "password": "piccolo123"}' \
    http://localhost:8000/login/

To get data from a protected endpoint:

curl -H "Authorization: Bearer your-token" \
    http://localhost:8000/private/movies/

Hint

You can use all HTTP methods by passing a valid token to the Authorization header.

Source

class piccolo_api.token_auth.endpoints.token_login(provider: TokenProvider = PiccoloTokenProvider())[source]

Create an endpoint for logging using tokens.

Parameters:

token_provider – Used to check if a token is valid.

class piccolo_api.token_auth.endpoints.PiccoloTokenProvider[source]

Retrieves a token from a Piccolo table.

class piccolo_api.token_auth.endpoints.TokenProvider[source]

Subclass this to provide your own custom token provider.