From 9780393b17de28c9bad6fecee4a5ce713541eb0f Mon Sep 17 00:00:00 2001 From: Anirban Kar Date: Fri, 7 Mar 2025 00:29:44 +0530 Subject: [PATCH] fix: git cookies are auto set anytime connects changed or loaded (#1461) --- .../tabs/connections/GithubConnection.tsx | 94 +++++++++++-------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/app/components/@settings/tabs/connections/GithubConnection.tsx b/app/components/@settings/tabs/connections/GithubConnection.tsx index 9f433724..36197f2e 100644 --- a/app/components/@settings/tabs/connections/GithubConnection.tsx +++ b/app/components/@settings/tabs/connections/GithubConnection.tsx @@ -77,6 +77,46 @@ export function GithubConnection() { const [isFetchingStats, setIsFetchingStats] = useState(false); const [isStatsExpanded, setIsStatsExpanded] = useState(false); + const fetchGithubUser = async (token: string) => { + try { + setIsConnecting(true); + + const response = await fetch('https://api.github.com/user', { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + throw new Error('Invalid token or unauthorized'); + } + + const data = (await response.json()) as GitHubUserResponse; + const newConnection: GitHubConnection = { + user: data, + token, + tokenType: connection.tokenType, + }; + + localStorage.setItem('github_connection', JSON.stringify(newConnection)); + Cookies.set('githubToken', token); + Cookies.set('githubUsername', data.login); + Cookies.set('git:github.com', JSON.stringify({ username: token, password: 'x-oauth-basic' })); + + setConnection(newConnection); + + await fetchGitHubStats(token); + + toast.success('Successfully connected to GitHub'); + } catch (error) { + logStore.logError('Failed to authenticate with GitHub', { error }); + toast.error('Failed to connect to GitHub'); + setConnection({ user: null, token: '', tokenType: 'classic' }); + } finally { + setIsConnecting(false); + } + }; + const fetchGitHubStats = async (token: string) => { try { setIsFetchingStats(true); @@ -182,51 +222,25 @@ export function GithubConnection() { setIsLoading(false); }, []); + useEffect(() => { + if (!connection) { + return; + } + + const token = connection.token; + const data = connection.user; + Cookies.set('githubToken', token); + Cookies.set('git:github.com', JSON.stringify({ username: token, password: 'x-oauth-basic' })); + + if (data) { + Cookies.set('githubUsername', data.login); + } + }, [connection]); if (isLoading || isConnecting || isFetchingStats) { return ; } - const fetchGithubUser = async (token: string) => { - try { - setIsConnecting(true); - - const response = await fetch('https://api.github.com/user', { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - - if (!response.ok) { - throw new Error('Invalid token or unauthorized'); - } - - const data = (await response.json()) as GitHubUserResponse; - const newConnection: GitHubConnection = { - user: data, - token, - tokenType: connection.tokenType, - }; - - localStorage.setItem('github_connection', JSON.stringify(newConnection)); - Cookies.set('githubToken', token); - Cookies.set('githubUsername', data.login); - Cookies.set('git:github.com', JSON.stringify({ username: token, password: 'x-oauth-basic' })); - - setConnection(newConnection); - - await fetchGitHubStats(token); - - toast.success('Successfully connected to GitHub'); - } catch (error) { - logStore.logError('Failed to authenticate with GitHub', { error }); - toast.error('Failed to connect to GitHub'); - setConnection({ user: null, token: '', tokenType: 'classic' }); - } finally { - setIsConnecting(false); - } - }; - const handleConnect = async (event: React.FormEvent) => { event.preventDefault(); await fetchGithubUser(connection.token);