@shogun-sdk/accounts
Version:
Shogun with Turnkey: configs, encryption, authentication with Telegram/Turnkey OIDC, etc.
44 lines (38 loc) • 1.21 kB
text/typescript
import { TelegramUser } from '../types/turnkey.js';
import { AuthDataValidator, objectToAuthDataMap } from '@telegram-auth/server';
const ONE_MINUTE_IN_SECONDS = 60;
export async function validateLogin(params: TelegramUser, botToken: string) {
const validator = new AuthDataValidator({ botToken });
try {
// Check if auth_date is older than 1 minute
const currentTimestamp = Math.floor(Date.now() / 1000);
const authDate = Number(params.auth_date);
if (currentTimestamp - authDate > ONE_MINUTE_IN_SECONDS) {
return {
success: false,
error: 'Authentication data is expired (older than 1 minute)',
};
}
const data = objectToAuthDataMap(params as unknown as Record<string, string | number>);
const user = await validator.validate(data);
if (user) {
return {
success: true,
user: {
...user,
hash: params.hash,
},
};
} else {
return {
success: false,
user: null,
};
}
} catch (error) {
return {
success: false, // Changed to false since this is an error case
error: error instanceof Error ? error.message : 'Unknown Error',
};
}
}