Skip to content

Commit

Permalink
Allow using cookies to authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcrammer committed Sep 23, 2024
1 parent 60cea08 commit 74d713a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@babel/preset-env": "^7.23.2",
"@sendgrid/mail": "^7.7.0",
"axios": "^1.6.2",
"cookie-parser": "^1.4.6",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
Expand Down
6 changes: 4 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import "./config/startup"

import Express, { json } from "express"
import sequelize from "./database/database"
import cookieParser from "cookie-parser"

const app = Express()
const PORT = process.env.PORT || 3000

app.use(json())
app.use(cookieParser())

import baseRoutes from "./routes/base"
import paymentRoutes from "./routes/payment"
import cardRoutes from "./routes/card"
Expand All @@ -18,8 +22,6 @@ import statusRoutes from "./routes/status"
import cronRoutes from "./routes/cron"
import logRoutes from "./routes/log"

app.use(json()) // middleware to parse json data

app.use("/", baseRoutes)
app.use("/payment", paymentRoutes)
app.use("/card", cardRoutes)
Expand Down
9 changes: 9 additions & 0 deletions src/middleware/digestMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { v4 as uuid } from "uuid"

export default function digest(req, res, next) {
// adds unique id to each request
// used for logging to track the request
req.id = uuid()

next()
}
2 changes: 1 addition & 1 deletion src/utils/getUser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from "node-fetch"

export default async function getUser(req, user_id, options) {
const api_token = req.headers["x-api-token"]
const api_token = req.headers["x-api-token"] || req?.cookies?._api_token
const { allowSelf = false } = options || {}

try {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// custom log function
// auto includes the user_id and request id

export default function log(req, message, data) {}

0 comments on commit 74d713a

Please sign in to comment.