-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0d79a68
Showing
34 changed files
with
11,443 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
.vscode | ||
**/.env | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sh hooks/pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
JWT_SECRET=test_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const express = require("express"); | ||
const bodyParser = require("body-parser"); | ||
const cors = require("cors"); | ||
const gamesRouter = require('./routes/games') | ||
const tokenChecker = require("./middleware/tokenChecker"); | ||
|
||
|
||
const app = express(); | ||
|
||
// Allow requests from any client | ||
// docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS | ||
// docs: https://expressjs.com/en/resources/middleware/cors.html | ||
app.use(cors()); | ||
|
||
// Parse JSON request bodies, made available on `req.body` | ||
app.use(bodyParser.json()); | ||
|
||
// API Routes | ||
app.use('/game', tokenChecker, gamesRouter) | ||
|
||
|
||
// 404 Handler | ||
app.use((_req, res) => { | ||
res.status(404).json({ err: "Error 404: Not Found" }); | ||
}); | ||
|
||
// Error handler | ||
app.use((err, _req, res, _next) => { | ||
console.error(err); | ||
if (process.env.NODE_ENV === "development") { | ||
res.status(500).send(err.message); | ||
} else { | ||
res.status(500).json({ err: "Something went wrong" }); | ||
} | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
function create() { | ||
|
||
} | ||
|
||
const GamesController = { | ||
create: create | ||
}; | ||
|
||
module.exports = GamesController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// docs: https://github.com/motdotla/dotenv#%EF%B8%8F-usage | ||
require("dotenv").config(); | ||
|
||
const app = require("./app.js"); | ||
|
||
|
||
function listenForRequests() { | ||
const port = process.env.PORT || 3000; | ||
app.listen(port, () => { | ||
console.log("Now listening on port", port); | ||
}); | ||
} | ||
|
||
listenForRequests(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const dotenv = require("dotenv"); | ||
dotenv.config({ path: "./.env.test" }); | ||
|
||
/** @type {import('jest').Config} */ | ||
const config = { | ||
verbose: true, // Give more useful output | ||
maxWorkers: 1, // Make sure our tests run one after another | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"checkJs": true | ||
}, | ||
"exclude": ["node_modules", "**/node_modules/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const JWT = require("jsonwebtoken"); | ||
const secret = process.env.JWT_SECRET; | ||
|
||
/** | ||
* This function is used to generate a JWT authentication | ||
* token for a specific user. | ||
* JWTs in 100 Seconds: https://www.youtube.com/watch?v=UBUNrFtufWo | ||
*/ | ||
function generateToken(user_id) { | ||
return JWT.sign( | ||
{ | ||
user_id: user_id, | ||
iat: Math.floor(Date.now() / 1000), | ||
|
||
// Set the JWT token to expire in 100 minutes | ||
exp: Math.floor(Date.now() / 1000) + 10 * 60, | ||
}, | ||
secret | ||
); | ||
} | ||
|
||
function decodeToken(token) { | ||
return JWT.decode(token, secret); | ||
} | ||
|
||
module.exports = { generateToken, decodeToken }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const JWT = require("jsonwebtoken"); | ||
|
||
// Middleware function to check for valid tokens | ||
function tokenChecker(req, res, next) { | ||
let token; | ||
const authHeader = req.get("Authorization"); | ||
|
||
if (authHeader) { | ||
token = authHeader.slice(7); | ||
} | ||
|
||
JWT.verify(token, process.env.JWT_SECRET, (err, payload) => { | ||
if (err) { | ||
console.log(err); | ||
res.status(401).json({ message: "auth error" }); | ||
} else { | ||
// Add the user_id from the payload to the req object. | ||
req.user_id = payload.user_id; | ||
next(); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = tokenChecker; |
Oops, something went wrong.