From ea889758ee85a4392ec58a0c54a91f95955b3e3a Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 30 Mar 2023 10:03:07 +0300 Subject: [PATCH] 1.2.1 - Added decodedToken in useTuitioToken hook. The token is obtained directly from TuitioProvider's react context. --- README.md | 3 ++- package-lock.json | 34 ++++++++++++++--------------- package.json | 4 ++-- src/hooks/useTuitioToken.ts | 17 ++++++++++++--- tests/hooks/useTuitioToken.test.tsx | 18 +++++++++++++-- 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index fb5c364..af3dff9 100644 --- a/README.md +++ b/README.md @@ -38,4 +38,5 @@ All tests in the package can be executed by running: `npm test`. 1.0.1 - Added useTuitioClient default options 1.1.0 - In this version, the account logout method and the latest changes published by Tuitio were implemented 1.1.1 - Account logout bug fix -1.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API and the latest changes published by "@flare/tuitio-client". +1.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API and the latest changes published by "@flare/tuitio-client". +1.2.1 - Added decodedToken in useTuitioToken hook. The token is obtained directly from TuitioProvider's react context. diff --git a/package-lock.json b/package-lock.json index 803beb5..8468338 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@flare/tuitio-client-react", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@flare/tuitio-client-react", - "version": "1.2.0", + "version": "1.2.1", "license": "MIT", "dependencies": { - "@flare/tuitio-client": "^1.2.0" + "@flare/tuitio-client": "^1.2.1" }, "devDependencies": { "@testing-library/react": "^12.1.5", @@ -718,16 +718,16 @@ } }, "node_modules/@flare/js-utils": { - "version": "1.0.3", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", - "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" + "version": "1.0.4", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.4.tgz", + "integrity": "sha512-U55xP+4E9DYWWaq4YnlNoi/MeAw3dDvhWSPN2hqQiO08dBBGIdKFZDYSLOMSzc5ccqErbiAc+dCuJ+ZSjfkptA==" }, "node_modules/@flare/tuitio-client": { - "version": "1.2.0", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.0.tgz", - "integrity": "sha512-5h2hLH1kJXhSiQxsrj3lnXz45h3p/Z+liN0XCVEbkOEFU4NXhgxegeODo0X9P/++nPQOOmGbwDTUObkCkO2yJA==", + "version": "1.2.1", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.1.tgz", + "integrity": "sha512-74gnrw/AQDUObgUMy3DqGD4AdputIyRkk2EToi3cVIWVRSaoa2I9HmPi+qyfJkv4akrEH42rRSL3naN6vpnbIA==", "dependencies": { - "@flare/js-utils": "^1.0.3", + "@flare/js-utils": "^1.0.4", "axios": "^1.3.2" } }, @@ -7655,16 +7655,16 @@ "dev": true }, "@flare/js-utils": { - "version": "1.0.3", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", - "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" + "version": "1.0.4", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.4.tgz", + "integrity": "sha512-U55xP+4E9DYWWaq4YnlNoi/MeAw3dDvhWSPN2hqQiO08dBBGIdKFZDYSLOMSzc5ccqErbiAc+dCuJ+ZSjfkptA==" }, "@flare/tuitio-client": { - "version": "1.2.0", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.0.tgz", - "integrity": "sha512-5h2hLH1kJXhSiQxsrj3lnXz45h3p/Z+liN0XCVEbkOEFU4NXhgxegeODo0X9P/++nPQOOmGbwDTUObkCkO2yJA==", + "version": "1.2.1", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.1.tgz", + "integrity": "sha512-74gnrw/AQDUObgUMy3DqGD4AdputIyRkk2EToi3cVIWVRSaoa2I9HmPi+qyfJkv4akrEH42rRSL3naN6vpnbIA==", "requires": { - "@flare/js-utils": "^1.0.3", + "@flare/js-utils": "^1.0.4", "axios": "^1.3.2" } }, diff --git a/package.json b/package.json index eda4b13..ac96fb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@flare/tuitio-client-react", - "version": "1.2.0", + "version": "1.2.1", "description": "Tuitio client react is an npm package written in typescript that facilitates the integration of a react application with Tuitio.", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", @@ -48,7 +48,7 @@ "README.md" ], "dependencies": { - "@flare/tuitio-client": "^1.2.0" + "@flare/tuitio-client": "^1.2.1" }, "peerDependencies": { "react": "^16.14.0" diff --git a/src/hooks/useTuitioToken.ts b/src/hooks/useTuitioToken.ts index fa33df0..8423c97 100644 --- a/src/hooks/useTuitioToken.ts +++ b/src/hooks/useTuitioToken.ts @@ -1,11 +1,13 @@ // Copyright (c) 2023 Tudor Stanciu -import { useContext, useCallback } from "react"; +import { useContext, useCallback, useState, useEffect } from "react"; import { TuitioContext } from "../contexts"; +import { camelizeKeys } from "@flare/js-utils"; +import type { TuitioTokenData } from "@flare/tuitio-client"; const useTuitioToken = () => { const state = useContext(TuitioContext); - const token = state.auth.token; + const [decodedToken, setDecodedToken] = useState(); const validate = useCallback((): boolean => { const validUntil = state.auth.validUntil; @@ -16,9 +18,18 @@ const useTuitioToken = () => { return valid; }, [state.auth.validUntil]); + useEffect(() => { + if (decodedToken || !state.auth.token) return; + const decodedTokenBuffer = Buffer.from(state.auth.token, "base64"); + const decodedTokenString = decodedTokenBuffer.toString("utf-8"); + const decoded = camelizeKeys(JSON.parse(decodedTokenString)); + setDecodedToken(decoded); + }, [decodedToken, state.auth.token]); + + const token = state.auth.token; const valid: boolean = validate(); - return { token, validate, valid }; + return { token, validate, valid, decodedToken }; }; export { useTuitioToken }; diff --git a/tests/hooks/useTuitioToken.test.tsx b/tests/hooks/useTuitioToken.test.tsx index 2bfd139..37a02cd 100644 --- a/tests/hooks/useTuitioToken.test.tsx +++ b/tests/hooks/useTuitioToken.test.tsx @@ -5,9 +5,12 @@ import { renderHook } from "@testing-library/react-hooks"; import { TuitioContext } from "../../src/contexts"; import { useTuitioToken } from "../../src/hooks"; +const tokenMock = + "eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ=="; + describe("useTuitioToken: positive flow", () => { const testState = { - auth: { userName: "test-user", token: "mocked-token", validUntil: new Date() }, + auth: { userName: "test-user", token: tokenMock, validUntil: new Date() }, configuration: { tuitioUrl: null } @@ -21,7 +24,7 @@ describe("useTuitioToken: positive flow", () => { it("should return the correct token", () => { const { result } = renderHook(() => useTuitioToken(), { wrapper }); - expect(result.current.token).toEqual("mocked-token"); + expect(result.current.token).toEqual(tokenMock); }); it("should return false value for valid", () => { @@ -34,6 +37,17 @@ describe("useTuitioToken: positive flow", () => { const valid = result.current.validate(); expect(valid).toBe(true); }); + + it("should successfully decode token", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + const { decodedToken } = result.current; + expect(decodedToken?.userId).toEqual(1); + expect(decodedToken?.userName).toBe("tuitio.user"); + expect(decodedToken?.firstName).toBe("Tuitio"); + expect(decodedToken?.securityStamp).toBeDefined(); + expect(decodedToken?.lockStamp).toBeDefined(); + expect(decodedToken?.createdAt).toBeDefined(); + }); }); describe("useTuitioToken: negative flow", () => {