* refactor customer api * refactor slots api * hooks/customers: use invalidateQueries * refactor services api * optimize hooks queryKey * refactor orders api * typo refactor hooks * fix telegramId type (number) * fix bot with new api * rename customers masters & clients query * fix useClientsQuery & useMastersQuery query * new line after 'use client' & 'use server' directives
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
/* eslint-disable promise/prefer-await-to-then */
|
|
'use client';
|
|
|
|
import { initData, isMiniAppDark, useSignal } from '@telegram-apps/sdk-react';
|
|
import { signIn, useSession } from 'next-auth/react';
|
|
import { useTheme } from 'next-themes';
|
|
import { redirect } from 'next/navigation';
|
|
import { useEffect } from 'react';
|
|
|
|
export default function Auth() {
|
|
useTelegramTheme();
|
|
useAuth();
|
|
|
|
return null;
|
|
}
|
|
|
|
function useAuth() {
|
|
const initDataUser = useSignal(initData.user);
|
|
const { status } = useSession();
|
|
|
|
useEffect(() => {
|
|
if (!initDataUser?.id) return;
|
|
|
|
if (status === 'authenticated') {
|
|
redirect('/profile');
|
|
}
|
|
|
|
if (status === 'unauthenticated') {
|
|
signIn('telegram', {
|
|
callbackUrl: '/profile',
|
|
redirect: false,
|
|
telegramId: initDataUser.id,
|
|
}).then(() => redirect('/profile'));
|
|
}
|
|
}, [initDataUser?.id, status]);
|
|
}
|
|
|
|
function useTelegramTheme() {
|
|
const isDark = isMiniAppDark();
|
|
const { setTheme } = useTheme();
|
|
|
|
useEffect(() => {
|
|
setTheme(isDark ? 'dark' : 'light');
|
|
}, [isDark, setTheme]);
|
|
}
|