Skip to content

feat: implement programmatic auth API#1382

Open
mikecrab wants to merge 1 commit intofastify:mainfrom
mikecrab:crabtree-take-home
Open

feat: implement programmatic auth API#1382
mikecrab wants to merge 1 commit intofastify:mainfrom
mikecrab:crabtree-take-home

Conversation

@mikecrab
Copy link
Copy Markdown

@mikecrab mikecrab commented Mar 7, 2026

Implement promise based authenticateRequest() API that enables programmatic execution of Passport strategies

Key additions:

  • Returns an explicit AuthResult object containing the resolution of the auth attempt
  • Adds an AuthContext to capture and provide data for logging and metrics
  • Maintains backwards compatibility with hook logic
  • Includes demo-auth-server.ts to test the code

Testing
Demo server created
image

Calling the route with the invalid Api Key before logging in to get a session cookie results in both strategies failing
image

Calling the route with the valid API Key before logging in to get a session cookie results in the session key failing, then the API key authentication succeeding
image

If we then hit the login route
image

We now have a cookie with a valid session so if we attempt authentication with the valid or invalid key, authentication will pass since it attempts to use session before even using the API keys
image
image

If we then logout, our session will be cleared and we can see that we will fall back to the API key auth again and we get the original results above
image
image
image

Implement promise based authenticateRequest() API that enables programmatic execution of Passport strategies

Key additions:
- Returns an explicit `AuthResult` object containing the resolution of the auth  attempt
- Adds an AuthContext to capture and provide data for logging and metrics
- Maintains backwards compatibility with hook logic
- Includes demo-auth-server.ts to test the code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant