Skip to content

Commit 4783848

Browse files
committed
final commit 634
1 parent 26ef137 commit 4783848

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/hooks/useGitHubAuth.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,42 @@ export const useGitHubAuth = () => {
77
const [error, setError] = useState('');
88

99
const octokit = useMemo(() => {
10-
setError('');
1110
if (!username) return null;
1211
if(token){
1312
return new Octokit({ auth: token });
1413
}
1514
return new Octokit();
1615
}, [username, token]);
1716

17+
// Clear error when username or token changes, before validation runs
18+
useEffect(() => {
19+
setError('');
20+
}, [username, token]);
21+
1822
// Validate token format and authentication on mount or change
1923
useEffect(() => {
2024
if (!token || !username) {
21-
setError('');
2225
return;
2326
}
2427

28+
const controller = new AbortController();
29+
2530
const validateAuth = async () => {
2631
if (!octokit) return;
2732

2833
try {
2934
// Attempt a simple API call to verify the token is valid
30-
await octokit.request('GET /user');
31-
setError('');
35+
await octokit.request('GET /user', { request: { signal: controller.signal } });
36+
// Only update state if request was not aborted
37+
if (!controller.signal.aborted) {
38+
setError('');
39+
}
3240
} catch (err: unknown) {
41+
// Ignore if request was aborted
42+
if (controller.signal.aborted) {
43+
return;
44+
}
45+
3346
const error = err as {
3447
status?: number;
3548
message?: string;
@@ -49,7 +62,10 @@ export const useGitHubAuth = () => {
4962

5063
// Validate on token change (but with a small delay to avoid excessive API calls during typing)
5164
const timeoutId = setTimeout(validateAuth, 500);
52-
return () => clearTimeout(timeoutId);
65+
return () => {
66+
clearTimeout(timeoutId);
67+
controller.abort();
68+
};
5369
}, [token, username, octokit]);
5470

5571
const getOctokit = () => octokit;

0 commit comments

Comments
 (0)