Skip to content

Commit ef9c843

Browse files
implementing firebase etc
1 parent 7c8ae99 commit ef9c843

File tree

3 files changed

+127
-37
lines changed

3 files changed

+127
-37
lines changed

.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "sbhs-year-8-chess-tournament"
4+
}
5+
}

.gitignore

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env
67+
68+
# dataconnect generated files
69+
.dataconnect

app.js

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,58 @@ const firebaseConfig = {
88
storageBucket: "sbhs-year-8-chess-tournament.firebasestorage.app",
99
messagingSenderId: "632119283742",
1010
appId: "1:632119283742:web:ea2e5b59f5d80df1cede0f",
11-
};
12-
13-
firebase.initializeApp(firebaseConfig);
14-
const auth = firebase.auth();
15-
const db = firebase.database();
16-
17-
const signInBtn = document.getElementById("googleSignInBtn");
18-
const userInfo = document.getElementById("user-info");
19-
20-
signInBtn.addEventListener("click", () => {
21-
const provider = new firebase.auth.GoogleAuthProvider();
22-
auth.signInWithPopup(provider)
23-
.then((result) => {
24-
const user = result.user;
25-
const email = user.email;
26-
console.log(email);
27-
if (!email.endsWith("@student.sbhs.nsw.edu.au")) {
28-
alert("Please use your school Google account.");
29-
auth.signOut();
30-
return;
31-
}
32-
33-
// Add user to database
34-
const uid = user.uid;
35-
db.ref("users/" + uid).set({
36-
name: user.displayName,
37-
email: user.email,
38-
score: 0
39-
});
11+
};
12+
13+
firebase.initializeApp(firebaseConfig);
14+
const auth = firebase.auth();
15+
const db = firebase.database();
16+
17+
const signInBtn = document.getElementById("googleSignInBtn");
18+
const userInfo = document.getElementById("user-info");
19+
20+
signInBtn.addEventListener("click", () => {
21+
const provider = new firebase.auth.GoogleAuthProvider();
22+
auth.signInWithPopup(provider)
23+
.then((result) => {
24+
const user = result.user;
25+
const email = user.email;
26+
console.log(email);
27+
if (!email.endsWith("@student.sbhs.nsw.edu.au")) {
28+
alert("Please use your school Google account.");
29+
auth.signOut();
30+
return;
31+
}
32+
33+
// Add user to database
34+
const uid = user.uid;
35+
db.ref("users/" + uid).set({
36+
name: user.displayName,
37+
email: user.email,
38+
score: 0
39+
});
40+
41+
userInfo.innerText = `Signed in as ${user.displayName}`;
42+
signInBtn.style.display = "none";
43+
})
44+
.catch((error) => {
45+
console.error("Sign-in error:", error);
46+
alert("Sign-in failed.");
47+
});
48+
});
49+
50+
auth.onAuthStateChanged(async (user) => {
51+
if (user) {
52+
const uid = user.uid;
53+
const snapshot = await db.ref("users/" + uid).once("value");
54+
const userData = snapshot.val();
4055

41-
userInfo.innerText = `Signed in as ${user.displayName}`;
42-
signInBtn.style.display = "none";
43-
})
44-
.catch((error) => {
45-
console.error("Sign-in error:", error);
46-
alert("Sign-in failed.");
47-
});
48-
});
56+
if (userData && userData.currentGameUrl) {
57+
const link = document.createElement('a');
58+
link.href = userData.currentGameUrl;
59+
link.textContent = `Join your match as ${userData.lichessColor}`;
60+
link.target = "_blank";
61+
document.body.appendChild(link);
62+
}
63+
}
64+
});
4965

0 commit comments

Comments
 (0)