Overview
Prompts.dev uses OAuth 2.0 for user authentication. Users can sign in with GitHub or Google accounts. After successful authentication, a JWT token is issued for API access.Supported Providers
- GitHub - Uses scopes:
read:user,user:email - Google - Uses scopes:
openid,email,profile
OAuth Endpoints
Initiate Login
OAuth provider:
github or googleOptional CSRF state parameter. If not provided, a random state will be generated.
Set to
true for CLI authentication flow. Defaults to false for web flow.GET /v1/auth/:provider/login
The API sets a signed cookie
prompts_oauth_state containing the provider and state for CSRF protection. This cookie expires after 5 minutes.internal/auth/handler.go:31
OAuth Callback
OAuth provider:
github or googleCSRF state parameter returned by the OAuth provider
Authorization code returned by the OAuth provider
GET /v1/auth/:provider/callback
http://localhost:{CLI_OAUTH_PORT}/callback?token={jwt}&state={state}
For web flows, the token is also set as an HttpOnly cookie named
prompts_token.internal/auth/handler.go:68
Complete OAuth Flow
Redirect to Login
Direct users to the login endpoint:The server generates a random CSRF state token, signs it with HMAC-SHA256, and stores it in a secure cookie.
Provider Redirects to Callback
After authorization, the provider redirects back with
code and state parameters:Server Validates State
The server:
- Retrieves the signed state from the cookie
- Verifies the HMAC signature
- Compares the state parameter with the cookie value
401 UNAUTHORIZED.Reference: internal/auth/handler.go:84Exchange Code for Token
The server exchanges the authorization code for an OAuth access token with the provider.Reference:
internal/auth/service.go:72Fetch User Profile
Using the OAuth access token, the server fetches the user’s profile:
- GitHub: Calls
https://api.github.com/userand optionallyhttps://api.github.com/user/emails - Google: Calls
https://openidconnect.googleapis.com/v1/userinfo
internal/auth/service.go:181 and internal/auth/service.go:290Create or Link User
The server:
- Checks if an identity exists for this provider + provider user ID
- If found, retrieves the existing user
- If not found, checks for an existing user by verified email
- Creates a new user if needed
- Creates an identity record linking the user to the OAuth provider
internal/auth/service.go:88Issue JWT Token
A JWT token is generated containing the user’s ID and expiration time.Reference:
internal/auth/service.go:130CSRF Protection
The OAuth flow implements CSRF protection using a signed state parameter:- State Generation: A 32-byte random state is generated using
crypto/rand - State Signing: The state is concatenated with the provider name and signed using HMAC-SHA256
- Cookie Storage: The signed state is stored in an HttpOnly cookie
- Verification: On callback, the signature is verified and the state is compared
internal/auth/handler.go:139
Example: Complete GitHub OAuth Flow
CLI Authentication Flow
For CLI tools, use thecli=true query parameter:
- Start a local HTTP server on the configured port (default from
CLI_OAUTH_PORTenv var) - Open the login URL in the user’s browser
- Listen for the callback request
- Extract the token from the query parameter
- Store the token securely for future API requests
internal/auth/handler.go:127
Error Responses
Unsupported Provider
Missing Code or State
Invalid State (CSRF)
OAuth Exchange Failed
Provider-Specific Details
GitHub
Scopes Requested:read:user- Read user profile informationuser:email- Read user email addresses
- User ID (required)
- Username (login)
- Avatar URL
- Email (from profile or emails endpoint)
- Email verification status
/user/emails endpoint and prioritizes primary verified emails.
Reference: internal/auth/service.go:160
openid- OpenID Connect authenticationemail- User’s email addressprofile- User’s profile information
- User ID (sub)
- Name
- Email verification status
- Picture URL
internal/auth/service.go:269