This example demonstrates cookie-based authentication using NpgsqlRest with PostgreSQL functions.
A complete login/logout flow where:
- Users table - Stores users with bcrypt-hashed passwords (using
pgcryptoextension) - Login - Validates credentials and sets an authentication cookie
- Logout - Clears the authentication cookie
- Who Am I - Returns the current user's identity from the session
| File | Purpose |
|---|---|
| R__1_example_3_tables.sql | Creates users table with 2 test users (alice/bob) |
| R__2_example_3_login.sql | Login function that verifies password and returns user claims |
| R__3_example_3_logout.sql | Logout function (authorized users only) |
| R__4_example_3_who_am_i.sql | Returns current user info from session claims |
| config.json | NpgsqlRest config enabling cookie auth |
| example3Api.ts | Auto-generated TypeScript client |
- The
loginfunction returns aschemecolumn set to'cookies'- this tells NpgsqlRest to set an auth cookie - User claims (
user_id,username,email) are stored in the cookie - Protected endpoints use the
authorizecomment annotation - Claims are automatically mapped to function parameters via
ParameterNameClaimsMappingin config
- alice / password123
- bob / password456
bun run devThen open http://127.0.0.1:8080 in your browser.