Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shammy642 committed Oct 14, 2024
0 parents commit 0d79a68
Show file tree
Hide file tree
Showing 34 changed files with 11,443 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.vscode
**/.env
node_modules
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sh hooks/pre-commit
1 change: 1 addition & 0 deletions api/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JWT_SECRET=test_secret
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
37 changes: 37 additions & 0 deletions api/app.js
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;
10 changes: 10 additions & 0 deletions api/controllers/games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

function create() {

}

const GamesController = {
create: create
};

module.exports = GamesController;
14 changes: 14 additions & 0 deletions api/index.js
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();
10 changes: 10 additions & 0 deletions api/jest.config.js
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;
6 changes: 6 additions & 0 deletions api/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"checkJs": true
},
"exclude": ["node_modules", "**/node_modules/*"]
}
26 changes: 26 additions & 0 deletions api/lib/token.js
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 };
24 changes: 24 additions & 0 deletions api/middleware/tokenChecker.js
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;
Loading

0 comments on commit 0d79a68

Please sign in to comment.