1.2.1 - Added decodeToken function. The token is obtained directly by the function from local storage. If the token is missing, the function returns null.

master
Tudor Stanciu 2023-03-29 20:01:58 +03:00
parent f38c2d9940
commit aba635b7cc
7 changed files with 940 additions and 903 deletions

View File

@ -39,4 +39,5 @@ All tests in the package can be executed by running: `npm test`.
1.0.3 - Added LICENSE file 1.0.3 - Added LICENSE file
1.0.4 - TuitioState's token property can be null 1.0.4 - TuitioState's token property can be null
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.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API. 1.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API.
1.2.1 - Added decodeToken function. The token is obtained directly by the function from local storage. If the token is missing, the function returns null.

1765
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@flare/tuitio-client", "name": "@flare/tuitio-client",
"version": "1.2.0", "version": "1.2.1",
"description": "Tuitio client is an npm package written in typescript that facilitates the integration of a javascript application with Tuitio.", "description": "Tuitio client is an npm package written in typescript that facilitates the integration of a javascript application with Tuitio.",
"main": "./lib/index.js", "main": "./lib/index.js",
"types": "./lib/index.d.ts", "types": "./lib/index.d.ts",
@ -54,7 +54,7 @@
"typescript": "^4.9.5" "typescript": "^4.9.5"
}, },
"dependencies": { "dependencies": {
"@flare/js-utils": "^1.0.3", "@flare/js-utils": "^1.0.4",
"axios": "^1.3.2" "axios": "^1.3.2"
} }
} }

View File

@ -0,0 +1,35 @@
/**
* @jest-environment jsdom
*/
// Copyright (c) 2023 Tudor Stanciu
import { decodeToken } from "../token";
import { localStorage } from "@flare/js-utils";
import { storageKeys } from "../constants";
const { setItem } = localStorage;
test("Decoded token must be null if the auth data does not exists in local storage", async () => {
const decodedToken = decodeToken();
expect(decodedToken).toBeNull();
});
test("Token must be successfully decoded", async () => {
const authData = {
token:
"eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ==",
validUntil: new Date(),
userName: "tuitio.user"
};
setItem(storageKeys.AUTH_DATA, authData);
const decodedToken = decodeToken();
expect(decodedToken?.userId).toBe(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

@ -2,8 +2,9 @@
import { TuitioClient } from "./client"; import { TuitioClient } from "./client";
import { fetch } from "./state"; import { fetch } from "./state";
import { decodeToken } from "./token";
export * from "./types"; export * from "./types";
export { TuitioClient, fetch }; export { TuitioClient, fetch, decodeToken };
export default TuitioClient; export default TuitioClient;

18
src/token.ts Normal file
View File

@ -0,0 +1,18 @@
// Copyright (c) 2023 Tudor Stanciu
import { localStorage, camelizeKeys } from "@flare/js-utils";
import { storageKeys } from "./constants";
import type { TuitioTokenData } from "./types";
const { getItem } = localStorage;
const decodeToken = (): TuitioTokenData | null => {
const authData = getItem(storageKeys.AUTH_DATA);
if (!authData) return null;
const { token } = authData;
const decodedTokenString = atob(token);
const decodedToken = camelizeKeys(JSON.parse(decodedTokenString));
return decodedToken;
};
export { decodeToken };

View File

@ -23,3 +23,18 @@ export type TuitioUserInfo = {
claim?: object; claim?: object;
contactOptions?: [TuitioUserContactOption]; contactOptions?: [TuitioUserContactOption];
}; };
export type TuitioTokenData = {
tokenId: string;
userId: number;
userName: string;
firstName: string;
lastName: string;
email?: string;
profilePictureUrl?: string;
securityStamp: string;
lockStamp: string;
createdAt?: Date;
expiresIn?: number;
claim?: object;
};