Skip to content

Commit

Permalink
Merge pull request #392 from torusresearch/feat/telegram-vue-example
Browse files Browse the repository at this point in the history
feat: custom auth vue app
  • Loading branch information
guru-web3 authored Feb 4, 2025
2 parents 917e8ba + 21ffcfb commit 3717a38
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/handlers/TelegramHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import base64url from "base64url";
import deepmerge from "deepmerge";

import { UX_MODE } from "../utils/enums";
import { broadcastChannelOptions, getTimeout, validateAndConstructUrl } from "../utils/helpers";
import { broadcastChannelOptions, getTimeout, objectToAuthDataMap, validateAndConstructUrl } from "../utils/helpers";
import log from "../utils/loglevel";
import PopupHandler from "../utils/PopupHandler";
import AbstractLoginHandler from "./AbstractLoginHandler";
Expand Down Expand Up @@ -60,13 +60,9 @@ export default class TelegramHandler extends AbstractLoginHandler {
this.finalURL = finalUrl;
}

objectToAuthDataMap(tgAuthenticationResult: string) {
return JSON.parse(atob(tgAuthenticationResult)) as { first_name: string; last_name: string; photo_url: string; username: string; id: number };
}

async getUserInfo(params: LoginWindowResponse): Promise<TorusVerifierResponse> {
const { idToken } = params;
const userInfo = this.objectToAuthDataMap(idToken);
const userInfo = objectToAuthDataMap(idToken);
const { photo_url: profileImage = "", first_name = "", last_name = "", id } = userInfo;
return {
email: "", // Telegram does not provide email
Expand Down
4 changes: 2 additions & 2 deletions src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ class CustomAuth {
if (hash && queryParameters) {
const { error, hashParameters, instanceParameters } = handleRedirectParameters(hash, queryParameters);
if (error) throw new Error(error);
const { access_token: accessToken, id_token: idToken, ...rest } = hashParameters;
const { access_token: accessToken, id_token: idToken, tgAuthResult, ...rest } = hashParameters;
// State has to be last here otherwise it will be overwritten
loginParams = { accessToken, idToken, ...rest, state: instanceParameters };
loginParams = { accessToken, idToken: idToken || tgAuthResult || "", ...rest, state: instanceParameters };
} else {
this.storageHelper.clearOrphanedLoginDetails();
if (this.config.uxMode === UX_MODE.REDIRECT) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ export const validateAndConstructUrl = (domain: string): URL => {
}
};

export const objectToAuthDataMap = (tgAuthenticationResult: string) => {
return JSON.parse(atob(tgAuthenticationResult)) as { first_name: string; last_name: string; photo_url: string; username: string; id: number };
};

export function isMobileOrTablet(): boolean {
const browser = Bowser.getParser(navigator.userAgent);
const platform = browser.getPlatform();
Expand Down

0 comments on commit 3717a38

Please sign in to comment.