Docs / Guides / JWT authentication

Feature 09 · Study guide

JWT authentication

Enable AuthRole + User, issue access/refresh tokens, and protect the admin UI.

What you will learn
  • How to enable auth in an existing project
  • Which endpoints are created
  • How roles affect the admin dashboard

1. Setup

$ tpy auth
$ pip install -r requirements.txt
$ tpy migrate && tpy seed
$ tpy serve

Default seeded login: admin@example.com / admin123 (super-admin).

2. What gets added

  • Schema models: AuthRole, User (password hashed).
  • Auth routes under /auth/*.
  • JWT access token (short-lived) + refresh token (longer-lived).
  • Logout revokes refresh server-side.

3. API map

EndpointPurpose
POST /auth/registerCreate user
POST /auth/loginReturn access + refresh
POST /auth/refreshRotate access using refresh
POST /auth/logoutRevoke refresh
GET /auth/meCurrent user profile

4. How a client uses tokens

  1. Login → store access + refresh securely.
  2. Send Authorization: Bearer <access> on protected routes.
  3. On 401, call refresh; if refresh fails, force login again.

5. Roles

Dashboard access: super-admin and developer. The admin role is API-oriented by default.