moved decoded token in separate react hook

master
Tudor Stanciu 2023-03-30 11:04:09 +03:00
parent ea889758ee
commit 79f0820441
5 changed files with 64 additions and 30 deletions

View File

@ -4,3 +4,4 @@ export * from "./useTuitioClient";
export * from "./useTuitioToken"; export * from "./useTuitioToken";
export * from "./useTuitioUser"; export * from "./useTuitioUser";
export * from "./useTuitioUserInfo"; export * from "./useTuitioUserInfo";
export * from "./useTuitioDecodedToken";

View File

@ -0,0 +1,23 @@
// Copyright (c) 2023 Tudor Stanciu
import { useContext, useState, useEffect } from "react";
import { TuitioContext } from "../contexts";
import { camelizeKeys } from "@flare/js-utils";
import type { TuitioTokenData } from "@flare/tuitio-client";
const useTuitioDecodedToken = () => {
const state = useContext(TuitioContext);
const [decodedToken, setDecodedToken] = useState<TuitioTokenData>();
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]);
return { decodedToken };
};
export { useTuitioDecodedToken };

View File

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

View File

@ -0,0 +1,35 @@
// Copyright (c) 2023 Tudor Stanciu
import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { TuitioContext } from "../../src/contexts";
import { useTuitioDecodedToken } from "../../src/hooks";
const tokenMock =
"eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ==";
describe("useTuitioDecodedToken tests", () => {
const testState = {
auth: { userName: "test-user", token: tokenMock, validUntil: new Date() },
configuration: {
tuitioUrl: null
}
};
testState.auth.validUntil.setHours(testState.auth.validUntil.getHours() + 1);
const wrapper = ({ children }: { children?: React.ReactNode }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
);
it("should successfully decode token", () => {
const { result } = renderHook(() => useTuitioDecodedToken(), { 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();
});
});

View File

@ -5,12 +5,9 @@ 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: tokenMock, validUntil: new Date() }, auth: { userName: "test-user", token: "mocked-token", validUntil: new Date() },
configuration: { configuration: {
tuitioUrl: null tuitioUrl: null
} }
@ -24,7 +21,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(tokenMock); expect(result.current.token).toEqual("mocked-token");
}); });
it("should return false value for valid", () => { it("should return false value for valid", () => {
@ -37,17 +34,6 @@ 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", () => {