1.2.1 - Added decodedToken in useTuitioToken hook. The token is obtained directly from TuitioProvider's react context.

master
Tudor Stanciu 2023-03-30 10:03:07 +03:00
parent b152a9dafb
commit ea889758ee
5 changed files with 51 additions and 25 deletions

View File

@ -39,3 +39,4 @@ All tests in the package can be executed by running: `npm test`.
1.1.0 - In this version, the account logout method and the latest changes published by Tuitio were implemented 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.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.

34
package-lock.json generated
View File

@ -1,15 +1,15 @@
{ {
"name": "@flare/tuitio-client-react", "name": "@flare/tuitio-client-react",
"version": "1.2.0", "version": "1.2.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@flare/tuitio-client-react", "name": "@flare/tuitio-client-react",
"version": "1.2.0", "version": "1.2.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@flare/tuitio-client": "^1.2.0" "@flare/tuitio-client": "^1.2.1"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/react": "^12.1.5", "@testing-library/react": "^12.1.5",
@ -718,16 +718,16 @@
} }
}, },
"node_modules/@flare/js-utils": { "node_modules/@flare/js-utils": {
"version": "1.0.3", "version": "1.0.4",
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.4.tgz",
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" "integrity": "sha512-U55xP+4E9DYWWaq4YnlNoi/MeAw3dDvhWSPN2hqQiO08dBBGIdKFZDYSLOMSzc5ccqErbiAc+dCuJ+ZSjfkptA=="
}, },
"node_modules/@flare/tuitio-client": { "node_modules/@flare/tuitio-client": {
"version": "1.2.0", "version": "1.2.1",
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.0.tgz", "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.1.tgz",
"integrity": "sha512-5h2hLH1kJXhSiQxsrj3lnXz45h3p/Z+liN0XCVEbkOEFU4NXhgxegeODo0X9P/++nPQOOmGbwDTUObkCkO2yJA==", "integrity": "sha512-74gnrw/AQDUObgUMy3DqGD4AdputIyRkk2EToi3cVIWVRSaoa2I9HmPi+qyfJkv4akrEH42rRSL3naN6vpnbIA==",
"dependencies": { "dependencies": {
"@flare/js-utils": "^1.0.3", "@flare/js-utils": "^1.0.4",
"axios": "^1.3.2" "axios": "^1.3.2"
} }
}, },
@ -7655,16 +7655,16 @@
"dev": true "dev": true
}, },
"@flare/js-utils": { "@flare/js-utils": {
"version": "1.0.3", "version": "1.0.4",
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.3.tgz", "resolved": "https://lab.code-rove.com/public-node-registry/@flare/js-utils/-/js-utils-1.0.4.tgz",
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" "integrity": "sha512-U55xP+4E9DYWWaq4YnlNoi/MeAw3dDvhWSPN2hqQiO08dBBGIdKFZDYSLOMSzc5ccqErbiAc+dCuJ+ZSjfkptA=="
}, },
"@flare/tuitio-client": { "@flare/tuitio-client": {
"version": "1.2.0", "version": "1.2.1",
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.0.tgz", "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.1.tgz",
"integrity": "sha512-5h2hLH1kJXhSiQxsrj3lnXz45h3p/Z+liN0XCVEbkOEFU4NXhgxegeODo0X9P/++nPQOOmGbwDTUObkCkO2yJA==", "integrity": "sha512-74gnrw/AQDUObgUMy3DqGD4AdputIyRkk2EToi3cVIWVRSaoa2I9HmPi+qyfJkv4akrEH42rRSL3naN6vpnbIA==",
"requires": { "requires": {
"@flare/js-utils": "^1.0.3", "@flare/js-utils": "^1.0.4",
"axios": "^1.3.2" "axios": "^1.3.2"
} }
}, },

View File

@ -1,6 +1,6 @@
{ {
"name": "@flare/tuitio-client-react", "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.", "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", "main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js", "module": "./dist/esm/index.js",
@ -48,7 +48,7 @@
"README.md" "README.md"
], ],
"dependencies": { "dependencies": {
"@flare/tuitio-client": "^1.2.0" "@flare/tuitio-client": "^1.2.1"
}, },
"peerDependencies": { "peerDependencies": {
"react": "^16.14.0" "react": "^16.14.0"

View File

@ -1,11 +1,13 @@
// Copyright (c) 2023 Tudor Stanciu // Copyright (c) 2023 Tudor Stanciu
import { useContext, useCallback } from "react"; import { useContext, useCallback, useState, useEffect } from "react";
import { TuitioContext } from "../contexts"; import { TuitioContext } from "../contexts";
import { camelizeKeys } from "@flare/js-utils";
import type { TuitioTokenData } from "@flare/tuitio-client";
const useTuitioToken = () => { const useTuitioToken = () => {
const state = useContext(TuitioContext); const state = useContext(TuitioContext);
const token = state.auth.token; const [decodedToken, setDecodedToken] = useState<TuitioTokenData>();
const validate = useCallback((): boolean => { const validate = useCallback((): boolean => {
const validUntil = state.auth.validUntil; const validUntil = state.auth.validUntil;
@ -16,9 +18,18 @@ const useTuitioToken = () => {
return valid; return valid;
}, [state.auth.validUntil]); }, [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(); const valid: boolean = validate();
return { token, validate, valid }; return { token, validate, valid, decodedToken };
}; };
export { useTuitioToken }; export { useTuitioToken };

View File

@ -5,9 +5,12 @@ import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext } from "../../src/contexts"; import { TuitioContext } from "../../src/contexts";
import { useTuitioToken } from "../../src/hooks"; import { useTuitioToken } from "../../src/hooks";
const tokenMock =
"eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ==";
describe("useTuitioToken: positive flow", () => { describe("useTuitioToken: positive flow", () => {
const testState = { const testState = {
auth: { userName: "test-user", token: "mocked-token", validUntil: new Date() }, auth: { userName: "test-user", token: tokenMock, validUntil: new Date() },
configuration: { configuration: {
tuitioUrl: null tuitioUrl: null
} }
@ -21,7 +24,7 @@ describe("useTuitioToken: positive flow", () => {
it("should return the correct token", () => { it("should return the correct token", () => {
const { result } = renderHook(() => useTuitioToken(), { wrapper }); 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", () => { it("should return false value for valid", () => {
@ -34,6 +37,17 @@ describe("useTuitioToken: positive flow", () => {
const valid = result.current.validate(); const valid = result.current.validate();
expect(valid).toBe(true); 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", () => { describe("useTuitioToken: negative flow", () => {