check if github token is valid on page load and trigger re-auth flow if not (#120)

This commit is contained in:
Anastasiia Solop
2025-07-10 16:58:02 +02:00
committed by GitHub
parent 35b2631ba6
commit f8af65189f
6 changed files with 118 additions and 13 deletions

View File

@@ -22,7 +22,7 @@ import { GitHubLoginDialog } from '@/components/GitHubLoginDialog';
const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes);
function AppContent() {
const { config, updateConfig, loading } = useConfig();
const { config, updateConfig, loading, githubTokenInvalid } = useConfig();
const [showDisclaimer, setShowDisclaimer] = useState(false);
const [showOnboarding, setShowOnboarding] = useState(false);
const [showGitHubLogin, setShowGitHubLogin] = useState(false);
@@ -36,9 +36,12 @@ function AppContent() {
}
const notAuthenticated =
!config.github?.username || !config.github?.token;
setShowGitHubLogin(notAuthenticated);
setShowGitHubLogin(notAuthenticated || githubTokenInvalid);
}
}, [config]);
if (githubTokenInvalid) {
setShowGitHubLogin(true);
}
}, [config, githubTokenInvalid]);
const handleDisclaimerAccept = async () => {
if (!config) return;

View File

@@ -18,7 +18,7 @@ export function GitHubLoginDialog({
open: boolean;
onOpenChange: (open: boolean) => void;
}) {
const { config, loading } = useConfig();
const { config, loading, githubTokenInvalid } = useConfig();
const [fetching, setFetching] = useState(false);
const [error, setError] = useState<string | null>(null);
const [deviceState, setDeviceState] = useState<null | {
@@ -31,7 +31,9 @@ export function GitHubLoginDialog({
const [polling, setPolling] = useState(false);
const [copied, setCopied] = useState(false);
const isAuthenticated = !!(config?.github?.username && config?.github?.token);
const isAuthenticated =
!!(config?.github?.username && config?.github?.token) &&
!githubTokenInvalid;
const handleLogin = async () => {
setFetching(true);

View File

@@ -14,6 +14,7 @@ interface ConfigContextType {
updateAndSaveConfig: (updates: Partial<Config>) => void;
saveConfig: () => Promise<boolean>;
loading: boolean;
githubTokenInvalid: boolean;
}
const ConfigContext = createContext<ConfigContextType | undefined>(undefined);
@@ -25,6 +26,7 @@ interface ConfigProviderProps {
export function ConfigProvider({ children }: ConfigProviderProps) {
const [config, setConfig] = useState<Config | null>(null);
const [loading, setLoading] = useState(true);
const [githubTokenInvalid, setGithubTokenInvalid] = useState(false);
useEffect(() => {
const loadConfig = async () => {
@@ -45,6 +47,26 @@ export function ConfigProvider({ children }: ConfigProviderProps) {
loadConfig();
}, []);
// Check GitHub token validity after config loads
useEffect(() => {
if (loading) return;
const checkToken = async () => {
try {
const response = await fetch('/api/auth/github/check');
const data: ApiResponse<null> = await response.json();
if (!data.success && data.message === 'github_token_invalid') {
setGithubTokenInvalid(true);
} else {
setGithubTokenInvalid(false);
}
} catch (err) {
// If the check fails, assume token is invalid
setGithubTokenInvalid(true);
}
};
checkToken();
}, [loading]);
const updateConfig = useCallback((updates: Partial<Config>) => {
setConfig((prev) => (prev ? { ...prev, ...updates } : null));
}, []);
@@ -100,7 +122,14 @@ export function ConfigProvider({ children }: ConfigProviderProps) {
return (
<ConfigContext.Provider
value={{ config, updateConfig, saveConfig, loading, updateAndSaveConfig }}
value={{
config,
updateConfig,
saveConfig,
loading,
updateAndSaveConfig,
githubTokenInvalid,
}}
>
{children}
</ConfigContext.Provider>