2025-10-20 21:47:33 +08:00

50 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
export default function TwitterAuthCallbackPage() {
const router = useRouter();
useEffect(() => {
// 解析回调参数
const searchParams = new URLSearchParams(window.location.search);
const state = searchParams.get('state');
const code = searchParams.get('code');
// 将回调参数写入 localStorage供原窗口读取
if (state && code) {
try {
const payload = {
state,
code,
timestamp: Date.now(),
};
localStorage.setItem('twitterAuthCallbackPayload', JSON.stringify(payload));
// 通过变化一个随机标记键,确保触发其它窗口的 storage 事件
localStorage.setItem('twitterAuthCallbackFlag', Math.random().toString(36).slice(2));
} catch {
// 忽略本地存储异常
}
// 通过 postMessage 通知打开者(如果存在且同源策略允许)
try {
if (window.opener) {
window.opener.postMessage({ type: 'TWITTER_AUTH_CALLBACK', state, code }, window.location.origin);
}
} catch {
}
}
// 跳回应用页面(原窗口会通过 storage 事件弹出处理弹框)
router.push('/movies?twitterCallback=true');
}, [router]);
return (
<div className="flex items-center justify-center min-h-screen bg-gray-900 text-white">
<p> Twitter ...</p>
</div>
);
}