diff --git a/src/app/(landing)/page.tsx b/src/app/(landing)/page.tsx index 4fd128e..69e9b52 100644 --- a/src/app/(landing)/page.tsx +++ b/src/app/(landing)/page.tsx @@ -1,5 +1,6 @@ import Link from "next/link"; import { copy } from '@/shared/i18n'; +import { AuthLandingLoginButton } from "@/features/auth/components/AuthLandingLoginButton"; import { AuthRedirectButton } from "@/features/auth/components/AuthRedirectButton"; import { Button } from "@/shared/ui/Button"; @@ -19,7 +20,9 @@ export default function MarketingPage() { diff --git a/src/features/auth/components/AuthLandingLoginButton.tsx b/src/features/auth/components/AuthLandingLoginButton.tsx new file mode 100644 index 0000000..25dee13 --- /dev/null +++ b/src/features/auth/components/AuthLandingLoginButton.tsx @@ -0,0 +1,38 @@ +'use client'; + +import type { ReactNode } from 'react'; +import { useAuthStore } from '@/entities/auth'; +import { Button, type ButtonSize, type ButtonVariant } from '@/shared/ui/Button'; +import { hasClientAuth } from '../model/hasClientAuth'; + +interface AuthLandingLoginButtonProps { + children: ReactNode; + className?: string; + size?: ButtonSize; + variant?: ButtonVariant; +} + +export function AuthLandingLoginButton({ + children, + className, + size = 'sm', + variant = 'ghost', +}: AuthLandingLoginButtonProps) { + const accessToken = useAuthStore((state) => state.accessToken); + const isAuthenticated = useAuthStore((state) => state.isAuthenticated); + + if ( + hasClientAuth({ + accessToken, + isAuthenticated, + }) + ) { + return null; + } + + return ( + + ); +} diff --git a/src/features/auth/components/AuthRedirectButton.tsx b/src/features/auth/components/AuthRedirectButton.tsx index 083ee0c..c0d49cf 100644 --- a/src/features/auth/components/AuthRedirectButton.tsx +++ b/src/features/auth/components/AuthRedirectButton.tsx @@ -1,11 +1,10 @@ 'use client'; -import Cookies from 'js-cookie'; import { useRouter } from 'next/navigation'; import type { ReactNode } from 'react'; import { useAuthStore } from '@/entities/auth'; -import { TOKEN_COOKIE_KEY } from '@/shared/config/authTokens'; import { Button, type ButtonSize, type ButtonVariant } from '@/shared/ui/Button'; +import { hasClientAuth } from '../model/hasClientAuth'; interface AuthRedirectButtonProps { children: ReactNode; @@ -21,7 +20,7 @@ export function AuthRedirectButton({ className, size = 'md', variant = 'primary', - authenticatedHref = '/space', + authenticatedHref = '/app', unauthenticatedHref = '/login', }: AuthRedirectButtonProps) { const router = useRouter(); @@ -29,8 +28,14 @@ export function AuthRedirectButton({ const isAuthenticated = useAuthStore((state) => state.isAuthenticated); const handleClick = () => { - const hasAccessToken = isAuthenticated || Boolean(accessToken) || Boolean(Cookies.get(TOKEN_COOKIE_KEY)); - router.push(hasAccessToken ? authenticatedHref : unauthenticatedHref); + router.push( + hasClientAuth({ + accessToken, + isAuthenticated, + }) + ? authenticatedHref + : unauthenticatedHref, + ); }; return ( diff --git a/src/features/auth/model/hasClientAuth.ts b/src/features/auth/model/hasClientAuth.ts new file mode 100644 index 0000000..796741f --- /dev/null +++ b/src/features/auth/model/hasClientAuth.ts @@ -0,0 +1,13 @@ +'use client'; + +import Cookies from 'js-cookie'; +import { TOKEN_COOKIE_KEY } from '@/shared/config/authTokens'; + +interface HasClientAuthInput { + accessToken: string | null; + isAuthenticated: boolean; +} + +export const hasClientAuth = ({ accessToken, isAuthenticated }: HasClientAuthInput) => { + return isAuthenticated || Boolean(accessToken) || Boolean(Cookies.get(TOKEN_COOKIE_KEY)); +};