Skip to content

Commit

Permalink
feat(login-redirect): automatically react on redirect during login pr…
Browse files Browse the repository at this point in the history
…ocedure (fixes #7)
  • Loading branch information
khskekec committed Aug 12, 2022
1 parent 5765d37 commit 263f309
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 29 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
"start": "babel-watch --extensions \".ts\" src/index.ts",
"test": "babel-watch --extensions \".ts\" src/client.test.ts",
"debug": "babel-watch --inspect 0.0.0.0:9229 --extensions \".ts\" src/index.ts",
"lint": "eslint --fix --ext .ts ./src",
"format": "prettier --ignore-path .gitignore --write \"src/**/*.+(ts|json)\"",
Expand Down
14 changes: 14 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LibreLinkUpClient } from './client';

(async function() {
const username = 'USERNAME';
const password = 'PASSWORD';
const libreClient = LibreLinkUpClient({
username,
password,
connectionIdentifier: 'IDENTIFIER'
});

const data = await libreClient.read();
console.log(data);
})();
74 changes: 46 additions & 28 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import axios from 'axios';
import { LibreCgmData } from './types/client';
import { ActiveSensor, Connection, GlucoseItem } from './types/connection';
import { ConnectionsResponse, Datum } from './types/connections';
import { CountryResponse, AE, RegionalMap } from './types/countries';
import { GraphData } from './types/graph';
import { LoginResponse } from './types/login';
import { LoginResponse, LoginRedirectResponse } from './types/login';
import { mapData, trendMap } from './utils';

const LIBRE_LINK_SERVER = 'https://api.libreview.io';
const LIBRE_LINK_SERVER = 'https://api-us.libreview.io';

type ClientArgs = {
username: string;
Expand All @@ -28,6 +29,7 @@ type ReadResponse = {
const urlMap = {
login: '/llu/auth/login',
connections: '/llu/connections',
countries: '/llu/config/country?country=DE',
};

export const LibreLinkUpClient = ({
Expand Down Expand Up @@ -62,27 +64,43 @@ export const LibreLinkUpClient = ({
{ synchronous: true }
);

const login = async () => {
const loginResponse = await instance.post<LoginResponse>(urlMap.login, {
const login = async (): Promise<LoginResponse> => {
const loginResponse = await instance.post<LoginResponse | LoginRedirectResponse>(urlMap.login, {
email: username,
password,
});
jwtToken = loginResponse.data.data.authTicket.token;

return loginResponse;
if ((loginResponse.data as LoginRedirectResponse).data.redirect) {
const redirectResponse = loginResponse.data as LoginRedirectResponse;
const countryNodes = await instance.get<CountryResponse>(urlMap.countries);
const targetRegion = redirectResponse.data.region as keyof RegionalMap;
const regionDefinition: AE | undefined = countryNodes.data.data.regionalMap[targetRegion];

if (!regionDefinition) {
throw new Error(
`Unable to find region '${redirectResponse.data.region}'.
Available nodes are ${Object.keys(countryNodes.data.data.regionalMap).join(', ')}.`);
}

instance.defaults.baseURL = regionDefinition.lslApi;
return login();
}
jwtToken = (loginResponse.data as LoginResponse).data.authTicket.token;

return loginResponse.data as LoginResponse;
};

const loginWrapper =
<Return>(func: () => Promise<Return>) =>
async (): Promise<Return> => {
try {
if (!jwtToken) await login();
return func();
} catch (e) {
await login();
return func();
}
};
async (): Promise<Return> => {
try {
if (!jwtToken) await login();
return func();
} catch (e) {
await login();
return func();
}
};

const getConnections = loginWrapper<ConnectionsResponse>(async () => {
const response = await instance.get<ConnectionsResponse>(
Expand Down Expand Up @@ -171,19 +189,19 @@ export const LibreLinkUpClient = ({
);
const averageTrend =
trendMap[
parseInt(
(
Math.round(
(memValues.reduce(
(acc, cur) => acc + trendMap.indexOf(cur.trend),
0
) /
amount) *
100
) / 100
).toFixed(0),
10
)
parseInt(
(
Math.round(
(memValues.reduce(
(acc, cur) => acc + trendMap.indexOf(cur.trend),
0
) /
amount) *
100
) / 100
).toFixed(0),
10
)
];

mem = new Map();
Expand Down
26 changes: 26 additions & 0 deletions src/types/countries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface AE {
lslApi: string;
socketHub: string;
}

export interface RegionalMap {
us: AE;
eu: AE;
fr: AE;
jp: AE;
de: AE;
ap: AE;
au: AE;
ae: AE;
}

interface Data {
regionalMap: RegionalMap;
}

export interface CountryResponse {
status: number;
data: Data;
}


13 changes: 12 additions & 1 deletion src/types/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ export type LoginArgs = {
password: string;
};

export interface LoginRedirectResponse {
status: number;
data: LoginRedirectData;
}

interface LoginRedirectData {
redirect: boolean;
region: string;
}


export interface LoginResponse {
status: number;
data: Data;
Expand Down Expand Up @@ -66,7 +77,7 @@ interface Llu {
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Details {}
interface Details { }

interface System {
messages: SystemMessages;
Expand Down

0 comments on commit 263f309

Please sign in to comment.