Skip to content

Commit

Permalink
fix: add hidden button click
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerilym committed Jul 4, 2024
1 parent a7a0ab8 commit 702e33b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/auth/components/TelegramAuthButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Button } from '@session/ui/ui/button';
import { LoginButton } from '@telegram-auth/react';
import { forwardRef } from 'react';
import { forwardRef, useRef } from 'react';
import { TelegramIcon } from '../icons/TelegramIcon';
import { signIn, signOut, useSession } from '../lib/client';
import { ButtonDataTestId } from '../testing/data-test-ids';
Expand All @@ -14,16 +14,22 @@ type TelegramAuthButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
export const TelegramAuthButton = forwardRef<HTMLButtonElement, TelegramAuthButtonProps>(
({ csrfToken, ...props }, ref) => {
const { data, status } = useSession();
const buttonContainerRef = useRef<HTMLDivElement>(null);

const isConnected = status === 'authenticated';
const username = data?.user?.name;

console.log(data);

const handleButtonClick = () => {
const button = buttonContainerRef.current?.querySelector('button');
button?.click();
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleClick = (data: any) => {
if (!isConnected) {
signIn('telegram', { callbackUrl: window.location.href }, data);
signIn('telegram', {}, data);
} else {
signOut();
}
Expand All @@ -33,10 +39,10 @@ export const TelegramAuthButton = forwardRef<HTMLButtonElement, TelegramAuthButt
<>
<input name="csrfToken" type="hidden" defaultValue={csrfToken} />
<div className="hidden">
<LoginButton botUsername="session_testnet_bot" />
<LoginButton botUsername="session_testnet_bot" onAuthCallback={handleClick} />
</div>
<Button
onClick={handleClick}
onClick={handleButtonClick}
data-testid={ButtonDataTestId.TELEGRAM_AUTH}
ref={ref}
{...props}
Expand Down

0 comments on commit 702e33b

Please sign in to comment.