-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-github.js
More file actions
103 lines (87 loc) · 2.84 KB
/
app-github.js
File metadata and controls
103 lines (87 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const firebaseConfig = {
apiKey: "AIzaSyBdcqSSWmjGnn3fsi8eDQTXtFDFUiYbtfU",
authDomain: "devtrackr-149be.firebaseapp.com",
projectId: "devtrackr-149be",
storageBucket: "devtrackr-149be.firebasestorage.app",
messagingSenderId: "936962359530",
appId: "1:936962359530:web:0a27728821a2a02ea91793",
measurementId: "G-VX3H0BDW39",
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
const btnLogout = document.getElementById("btn-logout");
btnLogout.onclick = () => {
auth.signOut().then(() => {
window.location.href = "index.html";
});
};
auth.onAuthStateChanged((user) => {
if (!user) {
window.location.href = "index.html";
}
});
const githubUsernameInput = document.getElementById("github-username");
const btnSaveGithub = document.getElementById("btn-save-github");
const btnLoadGithub = document.getElementById("btn-load-github");
const githubReposList = document.getElementById("github-repos");
btnSaveGithub.onclick = async () => {
const user = auth.currentUser;
if (!user) return;
const username = githubUsernameInput.value.trim();
if (!username) return alert("Enter username");
await db.collection("users").doc(user.uid).set(
{
githubUsername: username,
},
{ merge: true }
);
alert("GitHub username saved");
};
async function loadGithubUsername() {
const user = auth.currentUser;
if (!user) return;
const docSnap = await db.collection("users").doc(user.uid).get();
if (docSnap.exists && docSnap.data().githubUsername) {
githubUsernameInput.value = docSnap.data().githubUsername;
}
}
btnLoadGithub.onclick = async () => {
const username = githubUsernameInput.value.trim();
if (!username) return alert("Enter username");
githubReposList.innerHTML = "Loading...";
const res = await fetch(`https://api.github.com/users/${username}/repos`);
const repos = await res.json();
githubReposList.innerHTML = "";
// Check if user exists (GitHub API returns error object for 404)
if (repos.message) {
const li = document.createElement("li");
li.textContent = "User not found";
li.style.color = "#f87171";
li.style.fontWeight = "600";
githubReposList.appendChild(li);
return;
}
if (!repos.length) {
const li = document.createElement("li");
li.textContent = "User exists but has no public repositories";
li.style.color = "#9ca3af";
li.style.fontStyle = "italic";
githubReposList.appendChild(li);
return;
}
(repos || []).slice(0, 6).forEach((r) => {
const last = r.pushed_at
? new Date(r.pushed_at).toDateString()
: "N/A";
const li = document.createElement("li");
li.textContent = `${r.name} — Last commit: ${last}`;
githubReposList.appendChild(li);
});
};
// Load username on page load
auth.onAuthStateChanged((user) => {
if (user) {
loadGithubUsername();
}
});