Skip to content

Commit

Permalink
feat(react): move from jsonwebtoken to jose
Browse files Browse the repository at this point in the history
This removes the reliance on node built in modules as they don't work in
a browser context without polyfilling.
  • Loading branch information
blurrah committed Feb 20, 2025
1 parent 3a9eeba commit 24aabc6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-cougars-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/federated-token-react": minor
---

Replace jsonwebtoken with jose as jsonwebtoken relies on built-in node modules.
16 changes: 11 additions & 5 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
"types": "./dist/index.d.ts"
}
},
"keywords": ["graphql", "authentication", "react"],
"keywords": [
"graphql",
"authentication",
"react"
],
"author": "Lab Digital <[email protected]>",
"license": "MIT",
"repository": {
Expand All @@ -29,15 +33,17 @@
"test:ci": "vitest run --coverage",
"tsc": "tsc --noEmit"
},
"files": ["dist", "src"],
"files": [
"dist",
"src"
],
"dependencies": {
"js-cookie": "3.0.5",
"jsonwebtoken": "9.0.2"
"jose": "5.4.0",
"js-cookie": "3.0.5"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/js-cookie": "3.0.6",
"@types/jsonwebtoken": "9.0.6",
"@types/react": "18.3.3",
"@vitest/coverage-v8": "1.6.0",
"node-mocks-http": "^1.16.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Auth context for the site
"use client";
import { decodeJwt } from "jose";
import Cookie from "js-cookie";
import { decode as jwtDecode } from "jsonwebtoken";
import {
createContext,
useCallback,
Expand Down Expand Up @@ -240,7 +240,7 @@ export function AuthProvider({
(acc, key) =>
// biome-ignore lint/performance/noAccumulatingSpread: fixme
skipKeys.includes(key) ? acc : { ...acc, [key]: tokenPayload[key] },
{},
{}
);
};
if (userToken) {
Expand Down Expand Up @@ -403,7 +403,7 @@ export function useAuth() {
}

export const decodeToken = (token: string): TokenPayload | undefined => {
const decodedToken = jwtDecode(token, { complete: true });
const decodedToken = decodeJwt(token);
if (
!decodedToken ||
!decodedToken.payload ||
Expand Down
97 changes: 3 additions & 94 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24aabc6

Please sign in to comment.