Skip to content

feat: Add useAccessToken and useTokenClaims hooks #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nicknisi
Copy link
Contributor

@nicknisi nicknisi commented May 28, 2025

Note

This is dependent on workos/authkit-js#87.

This pull request introduces new functionality for managing access tokens with automatic refresh and token claims extraction, alongside minor improvements to type imports and exports. The most significant changes include the addition of the useAccessToken and useTokenClaims hooks, updates to the src/index.ts exports, and a refinement in type usage for the User interface.

New functionality for access tokens:

  • src/accessToken.ts: Added the useAccessToken hook to manage access tokens with automatic refresh and error handling. It includes a reducer for token state management and utility functions for token parsing and refresh logic.
  • src/accessToken.ts: Added the useTokenClaims hook to extract claims from access tokens, enabling developers to access custom claims in a type-safe manner.

Updates to exports:

  • src/index.ts: Exported the newly added useAccessToken and useTokenClaims hooks to make them available for use in other parts of the application.

Type improvements:

  • src/state.ts: Changed the import of the User type from a regular import to a type import for better type-only dependency management.

@nicknisi nicknisi changed the title improve API add useAccessToken and useTokenClaims hooks May 28, 2025
@nicknisi nicknisi changed the title add useAccessToken and useTokenClaims hooks feat: Add useAccessToken and useTokenClaims hooks May 28, 2025
@nicknisi nicknisi marked this pull request as ready for review May 28, 2025 15:59
@nicknisi nicknisi requested review from dandorman and Copilot May 28, 2025 16:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds new hooks to manage access tokens and extract token claims, while updating exports and type imports for better type management.

  • Added useAccessToken for token management with automatic refresh and error handling.
  • Added useTokenClaims to extract token claims in a type-safe manner.
  • Updated index exports and refined type imports in state.ts.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
src/state.ts Changed the import of the User type to a type-only import for improved dependency usage.
src/index.ts Exported the new hooks useAccessToken and useTokenClaims.
src/accessToken.ts Added new hooks for access token handling including refresh logic and token claims parsing.
package.json Updated the @workos-inc/authkit-js dependency version to 0.12.0.

Copy link
Collaborator

@cmatheson cmatheson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have logic in authkit-react/authkit-js to manage the refresh

*
* @returns An object containing the access token, loading state, error state, and a refresh function.
*/
export function useAccessToken() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the traditional way to get claims in authkit-react is via the useAuth hook. i think we should just extend that to support custom claims instead of bolting on a new hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I was looking to bring closer parity to authkit-nextjs, having the same hooks in workos/authkit-nextjs#258 plus having a cleaner separation of concerns.

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.

2 participants