From 52c225ec19e84476ad23d7d43b19e2a268c28718 Mon Sep 17 00:00:00 2001 From: jimikajon Date: Fri, 27 Sep 2019 22:13:31 -0400 Subject: [PATCH 001/112] added proposal.md with basic desc and team member names --- proposal.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 proposal.md diff --git a/proposal.md b/proposal.md new file mode 100644 index 00000000..91de995f --- /dev/null +++ b/proposal.md @@ -0,0 +1,13 @@ +# AJAJA Games + +A game website which hosts many games of various types. + +## Team +- Andrew Bonaventura +- Andrew Markoski +- Andrew Nolan +- James Kajon +- James Plante + + + From 107eec057e4441c90e7c9d5efe8bc17c2a28ac0d Mon Sep 17 00:00:00 2001 From: jimikajon Date: Fri, 27 Sep 2019 22:23:28 -0400 Subject: [PATCH 002/112] added requirements to proposal --- proposal.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/proposal.md b/proposal.md index 91de995f..b8bcba49 100644 --- a/proposal.md +++ b/proposal.md @@ -10,4 +10,17 @@ A game website which hosts many games of various types. - James Plante +## Requirements +### Static + +Our website will have one main which a user can choose a game from. +And also a page for each game. + +### Dynamic + +All of our games will be run in JavaScript. + +### Server-side + +Our website will have game scores stored in a database. Users will need to sign in to save their score. \ No newline at end of file From b170bd189b8d69f33c8fe214e7a4c36d8341eb17 Mon Sep 17 00:00:00 2001 From: jimikajon Date: Fri, 27 Sep 2019 22:33:25 -0400 Subject: [PATCH 003/112] added games list to proposal --- proposal.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/proposal.md b/proposal.md index b8bcba49..2c6e99af 100644 --- a/proposal.md +++ b/proposal.md @@ -23,4 +23,31 @@ All of our games will be run in JavaScript. ### Server-side -Our website will have game scores stored in a database. Users will need to sign in to save their score. \ No newline at end of file +Our website will have game scores stored in a database. Users will need to sign in to save their score. + + +## Games + +### Currently Developing + +- Breakout +- Rocket Lander +- Running game (like Chrome dino run) +- Asteroids +- Sudoku + +### Possible Future + +- 3D running game +- Pinball +- Air Hockey (against simple AI) +- Billiards +- Tetris +- Tic-tac-toe +- Hangman +- Puzzle games +- Memory/match game +- Typing test + + + From c1b28697e86aade6ad7167646785a84ea4996924 Mon Sep 17 00:00:00 2001 From: jimikajon Date: Sat, 28 Sep 2019 00:51:36 -0400 Subject: [PATCH 004/112] added basic project structure --- .gitignore | 3 ++ app.js | 37 +++++++++++++++++++++++++ package.json | 11 ++++++++ public/assets/images/favicon-16x16.png | Bin 0 -> 472 bytes public/assets/images/favicon-32x32.png | Bin 0 -> 1117 bytes public/css/ex-game/ex-game.css | 0 public/css/style.css | 0 public/js/ex-game/ex-game.js | 0 public/js/main.js | 0 views/ex-game.hbs | 11 ++++++++ views/helpers.js | 10 +++++++ views/index.hbs | 2 ++ views/layouts/layout.hbs | 22 +++++++++++++++ 13 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 package.json create mode 100644 public/assets/images/favicon-16x16.png create mode 100644 public/assets/images/favicon-32x32.png create mode 100644 public/css/ex-game/ex-game.css create mode 100644 public/css/style.css create mode 100644 public/js/ex-game/ex-game.js create mode 100644 public/js/main.js create mode 100644 views/ex-game.hbs create mode 100644 views/helpers.js create mode 100644 views/index.hbs create mode 100644 views/layouts/layout.hbs diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..57195033 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.DS_Store +node_modules/ +package-lock.json diff --git a/app.js b/app.js new file mode 100644 index 00000000..231f7687 --- /dev/null +++ b/app.js @@ -0,0 +1,37 @@ +const express = require("express"), + hbs = require("express-handlebars"), + path = require("path"), + morgan = require("morgan"), + compression = require("compression"), + helmet = require("helmet"); + +const app = express(); +const port = process.env.PORT | 3000; + +// morgan logger +app.use(morgan("dev")); +app.use(compression()); +app.use(helmet()); + +// template engine setup (handlebars) +app.engine("hbs", hbs({helpers: require("./views/helpers.js").helpers, extname: "hbs", defaultLayout: "layout", layoutsDir: __dirname + "/views/layouts/"})); +// app.engine("hbs", hbs({extname: "hbs", defaultLayout: "layout", layoutsDir: __dirname + "/views/layouts/"})); +app.set("views", path.join(__dirname, "views")); +app.set("view engine", "hbs"); + +app.use(express.static("public")); + +app.get("/", function(req, res) { + res.render("index"); +}); + +app.get("/games/:gameId", function(req, res) { + // TODO: get game path and any needed context from db then render + if (req.params.gameId === "aGameId") { // temporary hard coded check + res.render("ex-game"); + } else { + res.sendStatus(404); + } +}); + +app.listen(port, () => console.log(`Listening on port ${port}`)); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 00000000..27fcc16c --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "fp", + "version": "1.0.0", + "dependencies": { + "compression": "^1.7.4", + "express": "^4.17.1", + "express-handlebars": "^3.1.0", + "helmet": "^3.21.1", + "morgan": "^1.9.1" + } +} diff --git a/public/assets/images/favicon-16x16.png b/public/assets/images/favicon-16x16.png new file mode 100644 index 0000000000000000000000000000000000000000..0794bd182d721a74dd75ae12738cf6d39b2dc40d GIT binary patch literal 472 zcmV;}0Vn>6P)Nkl6o;Q7|C19e}Gz4)?tYel$k*X0y0)XqU`v9djP{nDM zKYlAsk^9TwLN!W#1{p?pu$M+F6sRwVjIsFs(s11dn#m*){7*XimqD?K&}%?$0pa;+ zM6~}(z6(++&eDew!JOH$UAFCVRAU@<(fJ zT`ZO&v$J0O{zJ0aM#A9?xm*pp-a;bLVFGr#x=z}Uk85$ceqpy?P+wm_*PSROSgb`_ z-<+H9_4T(jG+c6eT8qPxXKSlD`wEZv;%|JhkX}W~_mJlhFqT<1TN?zt}!)x38A O0000K1b$bFhgEtT{Dec?~P3xm~+@on8>{)b2gw_jA{GmrmoJzs~tR&v~Bj@B6%* za|FvB8RdQm|1=;KDRv{-jKE*rE*U}4gOGQCpO7OB)V^<9j%o2sr9)SDpwWDYkVgUC zGxetw>`3ty4#y{4KhphQdzJ=}QKLi1kCEa7K)X!G^VKF1@-w8!r#92Rn2p5&q<^s; zp}meE>VG<(yG_!GoIJ$W?>?AsZy|uUukJzP(Fg>uiSrpFRvga#d~>8@w%xe^lt1^- zwmb*tE}vGMlS#9gm;s=~T=MIh(Y_RYN;m}ZuAG5C zp@Ph7N0H*owX(V*J3_vr0H{EU9M{IJTZi%Ve5x+Hxj^OJS35kR#i4Orlq%c>iAHHke>@OYTAG}Wv$gZsH*xF zPfv#`&&_>tgO>Qov71KtZ5C@lPu@bD2X zUrylc+1u3B>8YuiK4Khm02CMhKzO)~va&d`vwuUUo5WyvOO4&^5QKz`aqQS#nw!H_ zorDB4m6f|`Y>b!#ptLleii+6#9+8X zOG_B7tuJ%t%r982UJML)v2EKh>(@KfJW!MN-w>!tn3&jy!O+32TVYgI?%}|J2c)N) z=F);%qC--vY zN-Q^ThI9J#Z8gsn`-&f+eL)${)X2EX GAME + +{{!-- Game goes here --}} + + + +{{#section 'head'}} + {{!-- Add CSS and JS here --}} + + +{{/section}} \ No newline at end of file diff --git a/views/helpers.js b/views/helpers.js new file mode 100644 index 00000000..4f87e9ec --- /dev/null +++ b/views/helpers.js @@ -0,0 +1,10 @@ +const helpers = { + section: function(name, options) { + if (!this._sections) this._sections = {}; + this._sections[name] = options.fn(this); + return null; + }, +}; + + +module.exports.helpers = helpers; \ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs new file mode 100644 index 00000000..10283ae6 --- /dev/null +++ b/views/index.hbs @@ -0,0 +1,2 @@ +

Welcome

+Ex Game \ No newline at end of file diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs new file mode 100644 index 00000000..6f6ca55d --- /dev/null +++ b/views/layouts/layout.hbs @@ -0,0 +1,22 @@ + + + + AJAJA Gaming + + + + + + {{{_sections.head}}} + + + + + + +
+ {{{ body }}} +
+ + + \ No newline at end of file From 4b13abaaf746772c8a8603305be7aaeb4eca6393 Mon Sep 17 00:00:00 2001 From: jimikajon Date: Sat, 28 Sep 2019 01:01:17 -0400 Subject: [PATCH 005/112] added bootstrap and split up head section --- views/ex-game.hbs | 13 +++++++++---- views/layouts/layout.hbs | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/views/ex-game.hbs b/views/ex-game.hbs index 55e762c0..6eea4314 100644 --- a/views/ex-game.hbs +++ b/views/ex-game.hbs @@ -4,8 +4,13 @@ -{{#section 'head'}} - {{!-- Add CSS and JS here --}} +{{#section 'css'}} + {{!-- Add CSS here --}} - -{{/section}} \ No newline at end of file +{{/section}} + +{{#section 'js'}} +{{!-- Add JS here --}} + +{{/section}} + diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs index 6f6ca55d..fd4c761d 100644 --- a/views/layouts/layout.hbs +++ b/views/layouts/layout.hbs @@ -7,8 +7,14 @@ - {{{_sections.head}}} - + {{{_sections.css}}} + + + + + + + @@ -18,5 +24,13 @@ {{{ body }}} + +{{{_sections.js}}} + + + + + + \ No newline at end of file From 682a07a0b93c1fa0c4b953e802c64af3c04726b3 Mon Sep 17 00:00:00 2001 From: jimikajon Date: Sat, 28 Sep 2019 19:16:27 -0400 Subject: [PATCH 006/112] added authorization. added nav bar --- app.js | 34 ++- ...ee-firebase-adminsdk-pqgau-d8dfed19f8.json | 12 + package.json | 4 +- public/js/auth-forms.js | 238 ++++++++++++++++++ public/js/main.js | 0 routes/auth-routes.js | 45 ++++ routes/game-data-routes.js | 17 ++ views/layouts/layout.hbs | 49 +++- views/partials/includes/auth-sign-in.hbs | 9 + views/partials/includes/auth-sign-up.hbs | 13 + views/partials/includes/nav-bar.hbs | 33 +++ views/partials/layouts/modal-layout.hbs | 28 +++ 12 files changed, 474 insertions(+), 8 deletions(-) create mode 100644 cs4241-fp-26fee-firebase-adminsdk-pqgau-d8dfed19f8.json create mode 100644 public/js/auth-forms.js delete mode 100644 public/js/main.js create mode 100644 routes/auth-routes.js create mode 100644 routes/game-data-routes.js create mode 100644 views/partials/includes/auth-sign-in.hbs create mode 100644 views/partials/includes/auth-sign-up.hbs create mode 100644 views/partials/includes/nav-bar.hbs create mode 100644 views/partials/layouts/modal-layout.hbs diff --git a/app.js b/app.js index 231f7687..da9d6b15 100644 --- a/app.js +++ b/app.js @@ -3,11 +3,26 @@ const express = require("express"), path = require("path"), morgan = require("morgan"), compression = require("compression"), - helmet = require("helmet"); + helmet = require("helmet"), + bodyParser = require('body-parser'), + firebaseAdmin = require("firebase-admin"); const app = express(); const port = process.env.PORT | 3000; + + +const serviceAccount = require("./cs4241-fp-26fee-firebase-adminsdk-pqgau-d8dfed19f8.json"); + +firebaseAdmin.initializeApp({ + credential: firebaseAdmin.credential.cert(serviceAccount), + databaseURL: "https://cs4241-fp-26fee.firebaseio.com" +}); + +const authRouter = require('./routes/auth-routes'); +const gameDataRouter = require('./routes/game-data-routes'); + + // morgan logger app.use(morgan("dev")); app.use(compression()); @@ -21,10 +36,6 @@ app.set("view engine", "hbs"); app.use(express.static("public")); -app.get("/", function(req, res) { - res.render("index"); -}); - app.get("/games/:gameId", function(req, res) { // TODO: get game path and any needed context from db then render if (req.params.gameId === "aGameId") { // temporary hard coded check @@ -34,4 +45,17 @@ app.get("/games/:gameId", function(req, res) { } }); +app.get("/", function(req, res) { + res.render("index"); +}); + +app.use(bodyParser.json()); // to support JSON-encoded bodies +app.use(bodyParser.urlencoded({ // to support URL-encoded bodies + extended: false +})); + +app.use('/auth', authRouter); +app.use('/submit/game', gameDataRouter); + + app.listen(port, () => console.log(`Listening on port ${port}`)); \ No newline at end of file diff --git a/cs4241-fp-26fee-firebase-adminsdk-pqgau-d8dfed19f8.json b/cs4241-fp-26fee-firebase-adminsdk-pqgau-d8dfed19f8.json new file mode 100644 index 00000000..d3c43746 --- /dev/null +++ b/cs4241-fp-26fee-firebase-adminsdk-pqgau-d8dfed19f8.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "cs4241-fp-26fee", + "private_key_id": "d8dfed19f8cf65920e9cb7f35f93fcc82e529c24", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCW96jRUXowMeXt\nrZ60CFlJ1OtAW3Nkd123L45LeAd9w9VDvVlvsuLplGgnk0/+zObu/fnDWnthAIJl\nfpoaOitIjmt+EKB1z3F3iUYtFXBD0EZPOCzJhwGp4jdt7hSe7uGicSGcOKFzC8Ua\nf8MQYbbE4diFURsWZmrS+7lkULPz3sMJXnFAKTewtDlxHuzYGHoz8atVYyDxPUEl\nAwWSyKNUunjLPvyP80/2PGYc3ojQyxDYXF0aXnby5m+gPqZzDFxAvBkZ//5UHZd+\nYrN4P5nGpEK5vBmtA5CnKnIVyNtRr+fOxGqf3LvDwVC3No1sYHJ+U+urdp5y8bws\njH2wN2xfAgMBAAECggEABrN1hd0E/HCqkRM80Sbxt6pAyvmENLhrq37r5QOt6XIw\n3DHib2fUVKPmv6L1UQ0NTxxs4rP4eATKxPCKrJFabH0N3mmkhbYwhirMt6GU+x2a\nkVef/U4Fz+Gfrjx8I9XcpBY7dLC60d164UM8iFSeBT01HC3Ocv8WzMipoP9IDZIL\nQjAotnid0mQrF3dl0BoTylK+Q7KXL+1xlGWQNELf9jl5Ew1AEgtb74WLumqTYXFP\nxUbBwe2IGRgoR8gZWzIZCC9UtE5/Hrp/0DD0GTbHY2DulIBq/WmIL3NmsxyOtrMq\nlZkCVK0qtW2Ss4c2qWQ3/eqIWzfw9WSEy4uuKvcsyQKBgQDPOrQeQtrVybH1gOKe\nckuVURm70O4LiXMOzmrIoQ0iBALGra27TC677eu1ZufwDNWYj/1U2lnwRAO6i0ox\ng+2Nfu/fpcRf9oXjTK69JaHMznalE0ObiFx0usWb6+kcVltMhuW2vWTNQFb7TD93\nvfJL16NR76dKveag2br53+/2NQKBgQC6f0AGjDGUak9QM/RiJ4ELknM1cNhy6CFc\n243jjIW3QJMl+Ay2SE5kbvjXb3JCK00aEWk80MG60E3cmbe5BbJ0yxgXsWCKzyZP\nBKqFOXVaul9AxOt4Ff+WVjWnY5TIdqIGIwhVksg+iyKDFRT9KdIWn21uMbUWKfUA\nY5XJR2eawwKBgEj2r2KUQX7SpFD/j0sgjRJG2tuxRY9UVumYripz4i7e4KdZkXkH\n6tCKhdp6ICH6Mt6jnBvbQZ2CJRnyXy4t+F9AmW7Sa+Ep6JxObJv2exCkhUu8C6rg\niUUkWd17Y7lxgnXgbVM5Phuykq2qfBJKE9y2US/RRMLhOCncX6QJP/EdAoGAaLiL\neyG9tVj8sEx8LaGBFL5nctqqr9so4LtoMR3296+K8QcYHEL9weeOU2qA/ibSZJDi\nRwPxxLSzA+xM/df1HR/tp2Aylb88A6LbHXPmMcv1MKlij0vK+N/vwM5p/SagVFHX\nnDWPu47lEjYEUyPolEtxZy8CQC/PIyjIMv2lb4ECgYAbXO7BQ1lSXMJw0/NurX5l\nn3wKC3Piy6jXKu8m/sk+YSQscMkEO7Vhcr+qeGxkEFZxPuxbbfNp+GOcxx25wYxu\nDRVCAEztfHZxE8UQ4/d4hPmI3dQ2+IhtrNXFbyxbXICaboLuzWIAsGMEY4wH4Vp5\nNEvM3MDi6IGI7t0HjUElIQ==\n-----END PRIVATE KEY-----\n", + "client_email": "firebase-adminsdk-pqgau@cs4241-fp-26fee.iam.gserviceaccount.com", + "client_id": "110803306376730175613", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-pqgau%40cs4241-fp-26fee.iam.gserviceaccount.com" +} diff --git a/package.json b/package.json index 27fcc16c..1f027029 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "compression": "^1.7.4", "express": "^4.17.1", "express-handlebars": "^3.1.0", + "firebase-admin": "^8.6.0", "helmet": "^3.21.1", - "morgan": "^1.9.1" + "morgan": "^1.9.1", + "body-parser": "latest" } } diff --git a/public/js/auth-forms.js b/public/js/auth-forms.js new file mode 100644 index 00000000..79acec7e --- /dev/null +++ b/public/js/auth-forms.js @@ -0,0 +1,238 @@ +function submit(e, url, dataParser, handelResponse) { + console.log(e, url, dataParser, handelResponse); + e.preventDefault(); // prevent url form submission + + const jsonData = dataParser(); + const data = JSON.stringify(jsonData); + console.log("form data: ", data); + + if (jsonData.warningStatus === "failed") { + const warning = e.target.parentElement.parentElement.querySelector('div#warningAlerts span'); + warning.textContent = jsonData.warningMsg; + warning.parentElement.classList.remove('d-none'); + return false; + } + + let request = new Request(url, { + method: 'POST', + body: data, + headers: { + 'Content-Type': 'application/json', + }, + }); + + console.log(request); + + fetch(request) + .then((resp) => resp.json()) + .then(function( data ) { + // do something with the response + console.log( data ); + handelResponse(data); + }) + .catch(function (error) { + console.log( error ); + }); + console.log('done submitting'); + return false; +} + +function signInSubmit(e, dataParser, handelResponse) { + console.log(e, dataParser, handelResponse); + e.preventDefault(); // prevent url form submission + + const data = dataParser(); + console.log("signing in: ", data); + console.log( data ); + handelResponse(data); + +} + + + +// add model submit button events and model activation events +window.onload = async function() { + const submitUrl = '/submit'; + const submitAuthSignUpUrl = '/auth/sign-up'; + // bind sign in and sign up events + document.getElementById("signInSubmitBtn").onclick = ((e) => signInSubmit(e, parseSignInForm, handelSignInResponse)); + document.getElementById("signUpSubmitBtn").onclick = ((e) => submit(e, submitAuthSignUpUrl, parseSignUpForm, handelSignUpResponse)); + // bind page specific events + + if (document.getElementById("scoresSubmitBtn")) { + document.getElementById("scoresSubmitBtn").onclick = ((e) => submit(e, submitUrl, parseGameForm, handelGameResponse)); + } +}; + + +// CLOSE MODAL // + +// will remove all modal backdrops +function closeModal(modal) { + modal.querySelector('.modal-header > button.close').click(); + modal.querySelector('form').reset(); + modal.querySelector('.alert').classList.remove('d-none') +} + + +// PARSE FORM // + +function parseSignInForm() { + return { + email: document.getElementById("sign-in-email").value, + password: document.getElementById("sign-in-password").value, + } +} + +function parseSignUpForm() { + return { + name: document.getElementById("sign-up-name").value, + email: document.getElementById("sign-up-email").value, + password: document.getElementById("sign-up-password").value, + } +} + + +// HANDEL RESPONSE // + +// sign in's are done client side +async function handelSignInResponse(data) { + firebaseSignInEmailPassword(data.email, data.password) + .then(function() { + const modal = document.getElementById('signInFormModal'); + closeModal(modal); + console.log("you are now logged in"); + }) + .catch(function(error) { + console.log("sign in failed"); + console.log(error.message); + document.querySelector('#sign-in-alert > span').textContent = error.message; + document.getElementById('sign-in-alert').classList.remove('d-none'); + }); +} + +async function handelSignUpResponse(data) { + if (await firebaseSignInToken(data.customToken) === true) { + const modal = document.getElementById('signUpFormModal'); + closeModal(modal); + console.log("you are now logged in"); + } else { + console.log("sign up failed"); + console.log(data.failMsg); + if (data.failMsg) { + document.querySelector('#sign-up-alert > span').textContent = data.failMsg; + document.getElementById('sign-up-alert').classList.remove('d-none'); + } + } + // update(); +} + + +// FIREBASE AUTH // + +const auth = firebase.auth(); + +async function firebaseSignInToken(token) { + if (typeof token !== "string") { + // if token is bad dont bother trying to sign in + return false; + } + return await auth.signInWithCustomToken(token) + .then(function() { + return true; + }) + .catch(function(error) { + // Handle Errors here. + const errorCode = error.code; + const errorMessage = error.message; + return false; + }); +} + +async function firebaseSignInEmailPassword(email, password) { + if (typeof email !== "string" || typeof password !== "string") { + // if creds are bad dont bother trying to sign in + return false; + } + return auth.signInWithEmailAndPassword(email, password); +} + +async function firebaseSignOut() { + return await auth.signOut() + .then(function() { + console.log('user signed out'); + // update(); + }) +} + +// DISPLAY USER // + +firebase.auth().onAuthStateChanged(update); + +function update(user) { + if (user === null || user === undefined) { + user = auth.currentUser; + } + updateNavButtons(user); + updateNavName(user); +} + +function updateNavButtons(user) { + if (user === null || user === undefined) { + document.getElementById('signUpBtn').classList.remove('d-none'); + document.getElementById('signInMenuDiv').classList.remove('d-none'); + document.getElementById('signOutBtn').classList.add('d-none'); + } else { + document.getElementById('signUpBtn').classList.add('d-none'); + document.getElementById('signInMenuDiv').classList.add('d-none'); + document.getElementById('signOutBtn').classList.remove('d-none'); + } +} + +function updateNavName(user) { + document.querySelector('nav> #userDisplayName').textContent = (user === null || user === undefined) ? "" : "Welcome " + user.displayName; +} + + +// OAuth // + +function googleSignIn() { + const provider = new firebase.auth.GoogleAuthProvider(); + // provider.addScope('https://www.googleapis.com/auth/contacts.readonly'); + provider.addScope('profile'); + provider.addScope('email'); + auth.signInWithPopup(provider).then(function(result) { + // This gives you a Google Access Token. You can use it to access the Google API. + const token = result.credential.accessToken; + // The signed-in user info. + const user = result.user; + console.log('Google user signed in ', result.credential.accessToken, result.user) + }).catch(function(error) { + if (error.code === 'auth/account-exists-with-different-credential') { + alert('You have signed up with a different provider for that email.'); + // Handle linking here if your app allows it. + } else { + console.log("Google sign in error", error) + } + }); +} + +function githubSignIn() { + const provider = new firebase.auth.GithubAuthProvider(); + provider.addScope('read:user'); + provider.addScope('user:email'); + auth.signInWithPopup(provider).then(function(result) { + console.log('Github user signed in ', result.credential.accessToken, result.user) + }).catch(function(error) { + if (error.code === 'auth/account-exists-with-different-credential') { + alert('You have signed up with a different provider for that email.'); + // Handle linking here if your app allows it. + } else { + console.error("Github sign in error", error); + } + }); + +} + + + diff --git a/public/js/main.js b/public/js/main.js deleted file mode 100644 index e69de29b..00000000 diff --git a/routes/auth-routes.js b/routes/auth-routes.js new file mode 100644 index 00000000..8b22b7d6 --- /dev/null +++ b/routes/auth-routes.js @@ -0,0 +1,45 @@ +const express = require('express'); +const authRouter = express.Router(); + +const firebaseAdmin = require("firebase-admin"); +const auth = firebaseAdmin.auth(); + +authRouter.post('/sign-up', async (req, res) => { + const data = req.body; + console.log(data); + auth.createUser({ + email: data.email, + emailVerified: false, + // phoneNumber: '+11234567890', + password: data.password, + displayName: data.name, + // photoURL: 'http://www.example.com/12345678/photo.png', + disabled: false + }) + .then(async function(userRecord) { + // See the UserRecord reference doc for the contents of userRecord. + console.log('Successfully created new user:', userRecord.uid); + await res.json({ + customToken: await getCustomToken(userRecord), + }); + }) + .catch(function(error) { + console.log('Error creating new user:', error); + res.json({ + failMsg: error.message, + }); + }); + +}); + +async function getCustomToken(user) { + return await firebaseAdmin.auth().createCustomToken(user.uid) + .then(function(customToken) { + return customToken; + }) + .catch(function(error) { + console.log('Error creating custom token:', error); + }); +} + +module.exports = authRouter; diff --git a/routes/game-data-routes.js b/routes/game-data-routes.js new file mode 100644 index 00000000..906e70f8 --- /dev/null +++ b/routes/game-data-routes.js @@ -0,0 +1,17 @@ +const express = require('express'); +const gameDataRouter = express.Router(); + +const firebaseAdmin = require("firebase-admin"); +const db = firebaseAdmin.firestore(); + +gameDataRouter.post("/", function(req, res) { + // TODO: write this to take in game id's + console.log("Game data submitted"); + console.log(req.body); +}); + + + + + +module.exports = gameDataRouter; diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs index fd4c761d..a15c530a 100644 --- a/views/layouts/layout.hbs +++ b/views/layouts/layout.hbs @@ -19,13 +19,58 @@ - + +{{> includes/nav-bar }} +
{{{ body }}}
- + +{{#> layouts/modal-layout type="signIn" title="Login" submitText="Login" }} + {{#*inline "form-content"}} + {{> includes/auth-sign-in }} + {{/inline}} +{{/layouts/modal-layout}} +{{#> layouts/modal-layout type="signUp" title="Sign Up" submitText="Sign Up" }} + {{#*inline "form-content"}} + {{> includes/auth-sign-up }} + {{/inline}} +{{/layouts/modal-layout}} + + + + + + + + + + {{{_sections.js}}} + + diff --git a/views/partials/includes/auth-sign-in.hbs b/views/partials/includes/auth-sign-in.hbs new file mode 100644 index 00000000..308c965e --- /dev/null +++ b/views/partials/includes/auth-sign-in.hbs @@ -0,0 +1,9 @@ + +
+ + + + +
diff --git a/views/partials/includes/auth-sign-up.hbs b/views/partials/includes/auth-sign-up.hbs new file mode 100644 index 00000000..443c945c --- /dev/null +++ b/views/partials/includes/auth-sign-up.hbs @@ -0,0 +1,13 @@ + +
+ + +
+
+ + + + +
diff --git a/views/partials/includes/nav-bar.hbs b/views/partials/includes/nav-bar.hbs new file mode 100644 index 00000000..e72b8eb2 --- /dev/null +++ b/views/partials/includes/nav-bar.hbs @@ -0,0 +1,33 @@ + diff --git a/views/partials/layouts/modal-layout.hbs b/views/partials/layouts/modal-layout.hbs new file mode 100644 index 00000000..a7f34e94 --- /dev/null +++ b/views/partials/layouts/modal-layout.hbs @@ -0,0 +1,28 @@ + \ No newline at end of file From 19137d3eaa2b038d758a4aff0044d80acb2278fe Mon Sep 17 00:00:00 2001 From: jimikajon Date: Sat, 28 Sep 2019 19:22:37 -0400 Subject: [PATCH 007/112] fixed all auth buttons flashing --- views/partials/includes/nav-bar.hbs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/views/partials/includes/nav-bar.hbs b/views/partials/includes/nav-bar.hbs index e72b8eb2..b7d572f3 100644 --- a/views/partials/includes/nav-bar.hbs +++ b/views/partials/includes/nav-bar.hbs @@ -6,10 +6,10 @@ From 12d725fc990fa96ca62bb13492ca53be401075ab Mon Sep 17 00:00:00 2001 From: jimikajon Date: Sat, 28 Sep 2019 19:38:08 -0400 Subject: [PATCH 008/112] added unlinked help and stats buttons to nav bar. made navbar blue --- views/partials/includes/nav-bar.hbs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/views/partials/includes/nav-bar.hbs b/views/partials/includes/nav-bar.hbs index b7d572f3..15948371 100644 --- a/views/partials/includes/nav-bar.hbs +++ b/views/partials/includes/nav-bar.hbs @@ -1,16 +1,23 @@ -