Commit
·
90847e7
1
Parent(s):
02621e3
Update ConnectionsTab.tsx
Browse fileschecks to make sure the entered username and token is correct before accepting it. Let you disconnect also.
app/components/settings/connections/ConnectionsTab.tsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import React, { useState } from 'react';
|
2 |
import { toast } from 'react-toastify';
|
3 |
import Cookies from 'js-cookie';
|
4 |
import { logStore } from '~/lib/stores/logs';
|
@@ -6,16 +6,76 @@ import { logStore } from '~/lib/stores/logs';
|
|
6 |
export default function ConnectionsTab() {
|
7 |
const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
|
8 |
const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
};
|
20 |
|
21 |
return (
|
@@ -28,7 +88,8 @@ export default function ConnectionsTab() {
|
|
28 |
type="text"
|
29 |
value={githubUsername}
|
30 |
onChange={(e) => setGithubUsername(e.target.value)}
|
31 |
-
|
|
|
32 |
/>
|
33 |
</div>
|
34 |
<div className="flex-1">
|
@@ -37,17 +98,41 @@ export default function ConnectionsTab() {
|
|
37 |
type="password"
|
38 |
value={githubToken}
|
39 |
onChange={(e) => setGithubToken(e.target.value)}
|
40 |
-
|
|
|
41 |
/>
|
42 |
</div>
|
43 |
</div>
|
44 |
-
<div className="flex mb-4">
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
</div>
|
52 |
</div>
|
53 |
);
|
|
|
1 |
+
import React, { useState, useEffect } from 'react';
|
2 |
import { toast } from 'react-toastify';
|
3 |
import Cookies from 'js-cookie';
|
4 |
import { logStore } from '~/lib/stores/logs';
|
|
|
6 |
export default function ConnectionsTab() {
|
7 |
const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
|
8 |
const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
|
9 |
+
const [isConnected, setIsConnected] = useState(false);
|
10 |
+
const [isVerifying, setIsVerifying] = useState(false);
|
11 |
|
12 |
+
useEffect(() => {
|
13 |
+
// Check if credentials exist and verify them
|
14 |
+
if (githubUsername && githubToken) {
|
15 |
+
verifyGitHubCredentials();
|
16 |
+
}
|
17 |
+
}, []);
|
18 |
+
|
19 |
+
const verifyGitHubCredentials = async () => {
|
20 |
+
setIsVerifying(true);
|
21 |
+
try {
|
22 |
+
const response = await fetch('https://api.github.com/user', {
|
23 |
+
headers: {
|
24 |
+
Authorization: `Bearer ${githubToken}`,
|
25 |
+
},
|
26 |
+
});
|
27 |
+
|
28 |
+
if (response.ok) {
|
29 |
+
const data = await response.json();
|
30 |
+
if (data.login === githubUsername) {
|
31 |
+
setIsConnected(true);
|
32 |
+
return true;
|
33 |
+
}
|
34 |
+
}
|
35 |
+
setIsConnected(false);
|
36 |
+
return false;
|
37 |
+
} catch (error) {
|
38 |
+
console.error('Error verifying GitHub credentials:', error);
|
39 |
+
setIsConnected(false);
|
40 |
+
return false;
|
41 |
+
} finally {
|
42 |
+
setIsVerifying(false);
|
43 |
+
}
|
44 |
+
};
|
45 |
+
|
46 |
+
const handleSaveConnection = async () => {
|
47 |
+
if (!githubUsername || !githubToken) {
|
48 |
+
toast.error('Please provide both GitHub username and token');
|
49 |
+
return;
|
50 |
+
}
|
51 |
+
|
52 |
+
setIsVerifying(true);
|
53 |
+
const isValid = await verifyGitHubCredentials();
|
54 |
+
|
55 |
+
if (isValid) {
|
56 |
+
Cookies.set('githubUsername', githubUsername);
|
57 |
+
Cookies.set('githubToken', githubToken);
|
58 |
+
logStore.logSystem('GitHub connection settings updated', {
|
59 |
+
username: githubUsername,
|
60 |
+
hasToken: !!githubToken,
|
61 |
+
});
|
62 |
+
toast.success('GitHub credentials verified and saved successfully!');
|
63 |
+
Cookies.set('git:github.com', JSON.stringify({ username: githubToken, password: 'x-oauth-basic' }));
|
64 |
+
setIsConnected(true);
|
65 |
+
} else {
|
66 |
+
toast.error('Invalid GitHub credentials. Please check your username and token.');
|
67 |
+
}
|
68 |
+
};
|
69 |
+
|
70 |
+
const handleDisconnect = () => {
|
71 |
+
Cookies.remove('githubUsername');
|
72 |
+
Cookies.remove('githubToken');
|
73 |
+
Cookies.remove('git:github.com');
|
74 |
+
setGithubUsername('');
|
75 |
+
setGithubToken('');
|
76 |
+
setIsConnected(false);
|
77 |
+
logStore.logSystem('GitHub connection removed');
|
78 |
+
toast.success('GitHub connection removed successfully!');
|
79 |
};
|
80 |
|
81 |
return (
|
|
|
88 |
type="text"
|
89 |
value={githubUsername}
|
90 |
onChange={(e) => setGithubUsername(e.target.value)}
|
91 |
+
disabled={isVerifying}
|
92 |
+
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor disabled:opacity-50"
|
93 |
/>
|
94 |
</div>
|
95 |
<div className="flex-1">
|
|
|
98 |
type="password"
|
99 |
value={githubToken}
|
100 |
onChange={(e) => setGithubToken(e.target.value)}
|
101 |
+
disabled={isVerifying}
|
102 |
+
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor disabled:opacity-50"
|
103 |
/>
|
104 |
</div>
|
105 |
</div>
|
106 |
+
<div className="flex mb-4 items-center">
|
107 |
+
{!isConnected ? (
|
108 |
+
<button
|
109 |
+
onClick={handleSaveConnection}
|
110 |
+
disabled={isVerifying || !githubUsername || !githubToken}
|
111 |
+
className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text disabled:opacity-50 disabled:cursor-not-allowed flex items-center"
|
112 |
+
>
|
113 |
+
{isVerifying ? (
|
114 |
+
<>
|
115 |
+
<div className="i-ph:spinner animate-spin mr-2" />
|
116 |
+
Verifying...
|
117 |
+
</>
|
118 |
+
) : (
|
119 |
+
'Connect'
|
120 |
+
)}
|
121 |
+
</button>
|
122 |
+
) : (
|
123 |
+
<button
|
124 |
+
onClick={handleDisconnect}
|
125 |
+
className="bg-bolt-elements-button-danger-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-danger-backgroundHover text-bolt-elements-button-danger-text"
|
126 |
+
>
|
127 |
+
Disconnect
|
128 |
+
</button>
|
129 |
+
)}
|
130 |
+
{isConnected && (
|
131 |
+
<span className="text-sm text-green-600 flex items-center">
|
132 |
+
<div className="i-ph:check-circle mr-1" />
|
133 |
+
Connected to GitHub
|
134 |
+
</span>
|
135 |
+
)}
|
136 |
</div>
|
137 |
</div>
|
138 |
);
|