mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-03-09 13:41:00 +00:00
fix: git cookies are auto set anytime connects changed or loaded (#1461)
This commit is contained in:
parent
20722a108c
commit
9780393b17
@ -77,6 +77,46 @@ export function GithubConnection() {
|
|||||||
const [isFetchingStats, setIsFetchingStats] = useState(false);
|
const [isFetchingStats, setIsFetchingStats] = useState(false);
|
||||||
const [isStatsExpanded, setIsStatsExpanded] = 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) => {
|
const fetchGitHubStats = async (token: string) => {
|
||||||
try {
|
try {
|
||||||
setIsFetchingStats(true);
|
setIsFetchingStats(true);
|
||||||
@ -182,51 +222,25 @@ export function GithubConnection() {
|
|||||||
|
|
||||||
setIsLoading(false);
|
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) {
|
if (isLoading || isConnecting || isFetchingStats) {
|
||||||
return <LoadingSpinner />;
|
return <LoadingSpinner />;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) => {
|
const handleConnect = async (event: React.FormEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
await fetchGithubUser(connection.token);
|
await fetchGithubUser(connection.token);
|
||||||
|
Loading…
Reference in New Issue
Block a user