Skip to content

Commit

Permalink
feat: getUserFromToken
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritanand committed Nov 25, 2023
1 parent 3ad86b1 commit b6f1f15
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const token =
const test = async () => {
const auth = new AuthLiteClient(apiKey, secretKey, orgId);
const user = await auth.getUser(token);
console.log({ user });

const uiol = await auth.getUserFromToken(user.access_token);
console.log({ user: uiol });
};

test();
42 changes: 42 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,48 @@ export class AuthLiteClient {
}
}

async getUserFromToken(accessToken: string): Promise<{
uid: string;
email: string;
img: string;
}> {
const url = "https://api.trustauthx.com/api/user/me/data";
const params = new URLSearchParams({
api_key: this.apiKey,
signed_key: this.signedKey,
AccessToken: accessToken,
});
const headers = { accept: "application/json" };

try {
const response = await fetch(url + "?" + params.toString(), {
method: "GET",
headers: headers,
});

if (response.status === 200) {
const data = await response.text();
const processedData = data.slice(1, -1);

const decoded = this.jwtDecode(processedData);

return {
uid: decoded?.uid,
email: decoded?.email,
img: decoded?.img,
};
} else {
throw new Error(
`Request failed with status code: ${
response.status
}\n${await response.text()}`
);
}
} catch (error) {
throw new Error(`Request failed: ${error}`);
}
}

async validateTokenSet(
access_token: string,
refresh_token: string
Expand Down

0 comments on commit b6f1f15

Please sign in to comment.