Skip to content

Commit

Permalink
Merge pull request #13 from ChangePlusPlusVandy/lyton/22
Browse files Browse the repository at this point in the history
feat: basic auth with auth0
  • Loading branch information
lolitaroz authored Nov 17, 2024
2 parents 95b86b1 + 5963288 commit 303a675
Show file tree
Hide file tree
Showing 21 changed files with 393 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

**/**/.env
33 changes: 33 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "src/server.ts",
"scripts": {
"build": "tsc",
"start": "nodemon ./dist/server.js",
"dev": "nodemon ./src/server.ts",
"start": "node src/index.ts",
"dev": "npx ts-node-dev --respawn --pretty --transpile-only src/server.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"prettier": "npx prettier --write .",
"format": "prettier --check ."
Expand All @@ -19,6 +19,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.0",
"express-oauth2-jwt-bearer": "^1.6.0",
"mongodb": "^6.9.0",
"mongoose": "^8.4.1",
"ts-node-dev": "^2.0.0",
Expand Down
35 changes: 35 additions & 0 deletions api/src/controllers/auth0-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Request, Response, NextFunction } from "express";
import {
InvalidTokenError,
UnauthorizedError,
} from "express-oauth2-jwt-bearer";

export const errorHandler = (
error: any,
request: Request,
response: Response,
next: NextFunction,
) => {
console.log("Auth Error");

if (error instanceof InvalidTokenError) {
const message = "Bad credentials";

response.status(error.status).json({ message });

return;
}

if (error instanceof UnauthorizedError) {
const message = "Requires authentication";

response.status(error.status).json({ message });

return;
}

const status = 500;
const message = "Internal Server Error";

response.status(status).json({ message });
};
21 changes: 21 additions & 0 deletions api/src/controllers/auth0-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { auth } from "express-oauth2-jwt-bearer";
import dotenv from "dotenv";
import path from "path";

dotenv.config({ path: path.resolve(__dirname, "../.env") });

const auth0Domain = process.env.AUTH0_DOMAIN;
const auth0Audience = process.env.AUTH0_AUDIENCE;

try {
if (!auth0Domain || !auth0Audience) {
throw new Error("AUTH0_DOMAIN or AUTH0_AUDIENCE is not set");
}
} catch (error) {
console.error(error);
}

export const validateAccessToken = auth({
issuerBaseURL: `https://${auth0Domain}`,
audience: auth0Audience,
});
11 changes: 11 additions & 0 deletions api/src/controllers/auth0-notFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request, Response, NextFunction } from "express";

export const notFoundHandler = (
request: Request,
response: Response,
next: NextFunction,
) => {
const message = "Not Found";

response.status(404).json({ message });
};
14 changes: 12 additions & 2 deletions api/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import express from "express";
import mongoose from "mongoose";
import dbConnect from "../config/db";
import sgMail from "@sendgrid/mail";
// import { validateAccessToken } from "../controllers/auth0-middleware";

const router = express.Router();

// TODO: Add auth0 middleware
// router.use(validateAccessToken);

// Call the dbConnect function to connect to MongoDB
dbConnect();

Expand Down Expand Up @@ -67,9 +71,15 @@ router.post("/create-user", async (req: any, res: any) => {
// Test route to check if the API is working
router.post("/test", async (req: any, res: any) => {
console.log("Received group data:");
const { name } = req.body;

return res.status(200).json({ name });
let name;
if (req.body.name === undefined) {
name = "empty";
} else {
({ name } = req.body);
}

return res.status(200).json(`Your name is ${name}`);
});

router.post("/send-email", async (req: any, res: any) => {
Expand Down
10 changes: 10 additions & 0 deletions api/src/routes/workshop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import express from "express";
import mongoose from "mongoose";
import dbConnect from "../config/db"; // Import the dbConnect function
// import { validateAccessToken } from "../controllers/auth0-middleware";

import { createWorkshop, getWorkshop } from "../controllers/workshopController";

const router = express.Router();

// TODO: Add auth0 middleware
// router.use(validateAccessToken);

// Call the dbConnect function to connect to MongoDB
dbConnect();

Expand Down Expand Up @@ -51,6 +55,12 @@ router.get(
},
);

router.post("/testId/:id", async (req: any, res: any) => {
res
.status(200)
.json({ message: "Workshop test successful", id: req.params.id });
});

// POPULATE VERSION (if details of mentor/mentee objects are needed on the frontend like name or picture)

// import express from 'express';
Expand Down
8 changes: 7 additions & 1 deletion api/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import dotenv from "dotenv";
import path from "path";

dotenv.config({ path: path.resolve(__dirname, "../.env") });

import express from "express";
import bodyParser from "body-parser";
import connectDB from "./config/db";
import { errorHandler } from "./controllers/auth0-errors";
import { notFoundHandler } from "./controllers/auth0-notFound";
import * as routes from "./routes/index";
import path from "path";

var cors = require("cors");

Expand All @@ -19,6 +22,9 @@ app.use("/workshop", routes.workshop);

connectDB();

app.use(notFoundHandler);
app.use(errorHandler);

app.listen(process.env.PORT || 8000, () =>
console.log(`Server running on port ${process.env.PORT || 8000}`),
);
1 change: 0 additions & 1 deletion app/.env

This file was deleted.

6 changes: 6 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REACT_APP_API_URL = http://127.0.0.1:8000

# auth
REACT_APP_AUTH0_DOMAIN=##check notion##
REACT_APP_AUTH0_CLIENT_ID=##check notion##
REACT_APP_AUTH0_CALLBACK_URL=http://localhost:3000/callback
55 changes: 55 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@auth0/auth0-react": "^2.2.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand All @@ -27,7 +28,9 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"format": "npx prettier --write \"src/**/*.{js,jsx,ts,tsx}\"",
"format-check": "npx prettier --check \"src/**/*.{js,jsx,ts,tsx}\""
},
"eslintConfig": {
"extends": [
Expand All @@ -46,5 +49,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "^3.3.3"
}
}
Loading

0 comments on commit 303a675

Please sign in to comment.