feat(auth): 랜딩 시작 분기와 앱 라우트 가드를 추가
This commit is contained in:
47
src/features/auth/components/AuthRedirectButton.tsx
Normal file
47
src/features/auth/components/AuthRedirectButton.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
"use client";
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
import Cookies from "js-cookie";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { TOKEN_COOKIE_KEY } from "@/features/auth/model/constants";
|
||||
import { Button, type ButtonSize, type ButtonVariant } from "@/shared/ui/Button";
|
||||
import { useAuthStore } from "@/store/useAuthStore";
|
||||
|
||||
interface AuthRedirectButtonProps {
|
||||
children: ReactNode;
|
||||
className?: string;
|
||||
size?: ButtonSize;
|
||||
variant?: ButtonVariant;
|
||||
authenticatedHref?: string;
|
||||
unauthenticatedHref?: string;
|
||||
}
|
||||
|
||||
export function AuthRedirectButton({
|
||||
children,
|
||||
className,
|
||||
size = "md",
|
||||
variant = "primary",
|
||||
authenticatedHref = "/space",
|
||||
unauthenticatedHref = "/login",
|
||||
}: AuthRedirectButtonProps) {
|
||||
const router = useRouter();
|
||||
const accessToken = useAuthStore((state) => state.accessToken);
|
||||
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||
|
||||
const handleClick = () => {
|
||||
const hasAccessToken = isAuthenticated || Boolean(accessToken) || Boolean(Cookies.get(TOKEN_COOKIE_KEY));
|
||||
router.push(hasAccessToken ? authenticatedHref : unauthenticatedHref);
|
||||
};
|
||||
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant={variant}
|
||||
size={size}
|
||||
className={className}
|
||||
onClick={handleClick}
|
||||
>
|
||||
{children}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
2
src/features/auth/model/constants.ts
Normal file
2
src/features/auth/model/constants.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export const TOKEN_COOKIE_KEY = "vr_access_token";
|
||||
export const REFRESH_TOKEN_COOKIE_KEY = "vr_refresh_token";
|
||||
Reference in New Issue
Block a user