hotfix(auth): missing session
This commit is contained in:
parent
363fce4499
commit
8092c7fecc
@ -2,12 +2,15 @@
|
||||
|
||||
import { authOptions } from '@/config/auth';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export async function getSessionUser() {
|
||||
const session = await getServerSession(authOptions);
|
||||
const user = session?.user;
|
||||
|
||||
if (!user?.telegramId) throw new Error('Missing session');
|
||||
if (!user?.telegramId) {
|
||||
return redirect('/');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@ -10,4 +10,6 @@ export default function Page() {
|
||||
|
||||
redirect(isTG ? '/telegram' : '/browser');
|
||||
});
|
||||
|
||||
return 'Redirecting...';
|
||||
}
|
||||
|
||||
@ -1,50 +1,79 @@
|
||||
/* eslint-disable promise/prefer-await-to-then */
|
||||
'use client';
|
||||
|
||||
import { LoadingSpinner } from '@repo/ui/components/ui/spinner';
|
||||
import { initData, isMiniAppDark, useSignal } from '@telegram-apps/sdk-react';
|
||||
import { signIn, useSession } from 'next-auth/react';
|
||||
import { signIn, type SignInResponse, useSession } from 'next-auth/react';
|
||||
import { useTheme } from 'next-themes';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
|
||||
export default function Auth() {
|
||||
useTelegramTheme();
|
||||
useAuth();
|
||||
|
||||
return null;
|
||||
useTelegramAuth();
|
||||
|
||||
return <LoadingSpinner />;
|
||||
}
|
||||
|
||||
function useAuth() {
|
||||
/**
|
||||
* Хук для авторизации пользователя через NextAuth
|
||||
*/
|
||||
function useTelegramAuth() {
|
||||
const initDataUser = useSignal(initData.user);
|
||||
const { status } = useSession();
|
||||
|
||||
const { data: session, status } = useSession();
|
||||
const router = useRouter();
|
||||
|
||||
const handleSignInResult = useCallback(
|
||||
(result: SignInResponse | undefined) => {
|
||||
if (!result) return;
|
||||
|
||||
if (
|
||||
result.error &&
|
||||
(result.error.includes('CredentialsSignin') || result.error.includes('UNREGISTERED'))
|
||||
) {
|
||||
router.replace('/unregistered');
|
||||
} else if (result.ok) {
|
||||
router.replace('/profile');
|
||||
}
|
||||
},
|
||||
[router],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initDataUser?.id) return;
|
||||
const telegramId = initDataUser?.id;
|
||||
if (!telegramId) return;
|
||||
|
||||
if (status === 'authenticated') {
|
||||
redirect('/profile');
|
||||
// Если telegramId есть в сессии — редирект
|
||||
if (session?.user?.telegramId) {
|
||||
router.replace('/profile');
|
||||
} else {
|
||||
// Если telegramId отсутствует — пробуем заново signIn
|
||||
void signIn('telegram', {
|
||||
callbackUrl: '/profile',
|
||||
redirect: false,
|
||||
telegramId: telegramId.toString(),
|
||||
}).then(handleSignInResult);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (status === 'unauthenticated') {
|
||||
signIn('telegram', {
|
||||
void signIn('telegram', {
|
||||
callbackUrl: '/profile',
|
||||
redirect: false,
|
||||
telegramId: initDataUser.id.toString(),
|
||||
}).then((result) => {
|
||||
if (
|
||||
result?.error &&
|
||||
(result?.error?.includes('CredentialsSignin') || result?.error?.includes('UNREGISTERED'))
|
||||
) {
|
||||
// Пользователь не зарегистрирован
|
||||
redirect('/unregistered');
|
||||
} else if (result?.ok) {
|
||||
redirect('/profile');
|
||||
}
|
||||
});
|
||||
telegramId: telegramId.toString(),
|
||||
}).then(handleSignInResult);
|
||||
}
|
||||
}, [initDataUser?.id, status]);
|
||||
}, [initDataUser?.id, status, session?.user?.telegramId, router, handleSignInResult]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Хук для установки темы из Telegram Mini App
|
||||
*/
|
||||
function useTelegramTheme() {
|
||||
const isDark = isMiniAppDark();
|
||||
const { setTheme } = useTheme();
|
||||
|
||||
@ -3,7 +3,7 @@ import { withAuth } from 'next-auth/middleware';
|
||||
|
||||
export default withAuth({
|
||||
callbacks: {
|
||||
authorized: ({ token }) => Boolean(token),
|
||||
authorized: ({ token }) => Boolean(token?.telegramId),
|
||||
},
|
||||
pages: {
|
||||
signIn: '/',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user