Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/hooks/useGitHubAuth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,64 @@
import { useState, useMemo } from 'react';
import { useState, useMemo, useEffect, useCallback, useRef } from 'react';
import { Octokit } from '@octokit/core';

// Inactivity timeout in milliseconds (30 minutes).
// If the user has not interacted with the application for this period,
// the session credentials are cleared automatically. This limits the
// window during which a stolen or leaked token remains usable.
const SESSION_TIMEOUT_MS = 30 * 60 * 1000;

export const useGitHubAuth = () => {
const [username, setUsername] = useState('');
const [token, setToken] = useState('');

const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

// Clear credentials on session expiry.
const clearSession = useCallback(() => {
setUsername('');
setToken('');
}, []);

// Reset the inactivity timer on every interaction.
// The timer is only active when a username is set (i.e., a session exists).
const resetTimer = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
if (username) {
timeoutRef.current = setTimeout(clearSession, SESSION_TIMEOUT_MS);
}
}, [username, clearSession]);

useEffect(() => {
if (!username) {
// No active session; clear any lingering timer.
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
return;
}

const events = ['mousemove', 'keydown', 'click', 'scroll', 'touchstart'];

// Start the timer immediately when a session begins.
resetTimer();

events.forEach((e) => window.addEventListener(e, resetTimer));

return () => {
events.forEach((e) => window.removeEventListener(e, resetTimer));
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, [username, resetTimer]);

const octokit = useMemo(() => {
if (!username) return null;
if(token){
return new Octokit({ auth: token });
if (token) {
return new Octokit({ auth: token });
}
return new Octokit();
}, [username, token]);
Expand All @@ -20,6 +70,7 @@ export const useGitHubAuth = () => {
setUsername,
token,
setToken,
clearSession,
getOctokit,
};
};
Loading