feat(登录): 添加登录重定向功能并优化路径处理

- 新增登录API路由,处理会话检查及重定向逻辑
- 在Sidebar组件中引入路径处理函数,优化注销后的重定向路径
- 更新LoginModal组件,为社交登录添加回调URL参数
This commit is contained in:
hex2077
2025-08-25 23:19:35 +08:00
parent ee6dfb0fe1
commit d7c4520a65
3 changed files with 31 additions and 4 deletions

View File

@@ -0,0 +1,22 @@
import { NextResponse, NextRequest } from 'next/server';
import { getSessionData } from "@/lib/server-actions";
export async function GET(request: NextRequest) {
const sessionData = await getSessionData();
let baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "/";
const pathname = request.nextUrl.searchParams.get('pathname');
if(!!pathname){
baseUrl += pathname.replace('/','');
}
// 如果没有获取到 session直接重定向到根目录
if (!sessionData?.user) {
const url = new URL(baseUrl, request.url);
return NextResponse.redirect(url);
}
// 创建一个 URL 对象,指向要重定向到的根目录
const url = new URL(baseUrl, request.url);
// 返回重定向响应
return NextResponse.redirect(url);
}

View File

@@ -63,7 +63,7 @@ const LoginModal: FC<LoginModalProps> = ({ isOpen, onClose, lang }) => {
<div className="space-y-4">
<button
onClick={() => signIn.social({ provider: "google" , newUserCallbackURL: "/api/newuser?provider=google&pathname=" + truePath})}
onClick={() => signIn.social({ provider: "google" , callbackURL: "/api/login?pathname=" + truePath, newUserCallbackURL: "/api/newuser?provider=google&pathname=" + truePath})}
className="w-full flex items-center justify-center gap-3 px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-lg font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors"
>
<AiOutlineChrome className="h-6 w-6" />
@@ -71,7 +71,7 @@ const LoginModal: FC<LoginModalProps> = ({ isOpen, onClose, lang }) => {
</button>
<button
onClick={() => signIn.social({ provider: "github" , newUserCallbackURL: "/api/newuser?provider=github&pathname=" + truePath })}
onClick={() => signIn.social({ provider: "github" , callbackURL: "/api/login?pathname=" + truePath, newUserCallbackURL: "/api/newuser?provider=github&pathname=" + truePath })}
className="w-full flex items-center justify-center gap-3 px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-lg font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors"
>
<AiOutlineGithub className="h-6 w-6" />

View File

@@ -22,6 +22,9 @@ import { cn } from '@/lib/utils';
import LoginModal from './LoginModal'; // 导入 LoginModal 组件
import type { PodcastItem } from '@/types';
import { useTranslation } from '../i18n/client'; // 导入 useTranslation
import { usePathname } from 'next/navigation'; // 导入 usePathname
import { getTruePathFromPathname } from '../lib/utils'; // 导入新函数
const enableTTSConfigPage = process.env.NEXT_PUBLIC_ENABLE_TTS_CONFIG_PAGE === 'true';
interface SidebarProps {
@@ -60,6 +63,8 @@ const Sidebar: React.FC<SidebarProps> = ({
const [session, setSession] = useState<any>(null); // 使用 useState 管理 session
const didFetch = useRef(false); // 使用 useRef 确保 useEffect 只在组件挂载时执行一次
const router = useRouter(); // 初始化 useRouter 钩子
const pathname = usePathname();
const truePath = getTruePathFromPathname(pathname, lang);
useEffect(() => {
// 首次加载时获取 session
@@ -85,7 +90,7 @@ const Sidebar: React.FC<SidebarProps> = ({
onSuccess: () => {
setSession(null); // 会话过期,注销成功后清空本地 session 状态
onCreditsChange(0); // 清空积分
router.push("/"); // 会话过期,执行注销并重定向到主页
router.push(truePath+"/"); // 会话过期,执行注销并重定向到主页
},
},
});
@@ -380,7 +385,7 @@ const Sidebar: React.FC<SidebarProps> = ({
setSession(null); // 注销成功后清空本地 session 状态
onPodcastExplore([]); // 注销后清空播客卡片
onCreditsChange(0); // 清空积分
router.push("/"); // 注销成功后重定向到主页
router.push(truePath+"/"); // 注销成功后重定向到主页
},
},
});