Skip to content

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented May 5, 2025

Add new withAuth function for simplified auth data access

Purpose

This PR adds a new withAuth function that provides a simpler way to access authentication data in child routes without wrapping loaders. It complements the existing authkitLoader pattern rather than replacing it.

Implementation

  • Added withAuth function that accepts LoaderFunctionArgs and returns auth data
  • Function reads from the session cookie without handling refresh operations
  • Added developer warning when no session cookie is found
  • Added token expiration warning to help detect potential timing issues
  • Maintained the same auth data structure returned by authkitLoader

Benefits

  • Reduces boilerplate in child routes that only need to read auth data
  • Simplifies access to user info, permissions, roles and tokens
  • Improves code readability by separating auth data access from loader logic
  • Works alongside existing patterns without breaking changes

Usage Example

export async function loader({ request }: LoaderFunctionArgs) {
  const { user, permissions } = await withAuth({ request });
  
  // Use auth data without wrapping the entire loader
  const data = await fetchSomeData();
  
  return json({ data });
}

Design Considerations

The withAuth function assumes that authkitLoader is used in a parent/root route to handle session refresh. It focuses on reading auth data rather than maintaining the authentication lifecycle.

Fixes #19

@nicknisi nicknisi requested a review from dandorman May 5, 2025 15:03
@nicknisi nicknisi merged commit eb23744 into main May 6, 2025
6 checks passed
@nicknisi nicknisi deleted the nicknisi/with-auth-helper branch May 6, 2025 20:31
This was referenced May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants