-
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
Ben Turner
authored and
Ben Turner
committed
Feb 14, 2024
1 parent
aadd2cb
commit 03ca2a4
Showing
12 changed files
with
189 additions
and
57 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 |
---|---|---|
@@ -1 +1 @@ | ||
# VS-attendance-api | ||
# VS-attendance-api |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,3 @@ | ||
import { config } from "dotenv"; | ||
|
||
config(); |
File renamed without changes.
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,18 @@ | ||
import { mockServer } from "../__tests__/utils"; | ||
import { decodeQRCodeDataUrl } from "../services/qr-code.service"; | ||
|
||
jest.mock("jsonwebtoken", () => ({ sign: () => "ACCESS_TOKEN" })); | ||
|
||
describe("qr-code-router.ts", () => { | ||
it("Should generate a QR code as a base64 encoded png", async () => { | ||
const response = await mockServer().get("/api/qr-code/generate"); | ||
expect(response.headers["content-type"]).toContain("application/json"); | ||
expect(response.body.dataUrl).toContain( | ||
"data:image/png;base64,iVBORw0KGgo", | ||
); | ||
const decoded = await decodeQRCodeDataUrl(response.body.dataUrl); | ||
expect(decoded).toBe( | ||
`${process.env.CLIENT_BASE_URL}?access_token=ACCESS_TOKEN`, | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import ex from "express"; | ||
import { generateQRCode } from "../services/qr-code.service"; | ||
import { sign } from "jsonwebtoken"; | ||
|
||
const qrCodeRouter = ex.Router(); | ||
|
||
qrCodeRouter.post("/generate", async (req, res) => { | ||
const dataUrl = await generateQRCode(req.body.userData); | ||
qrCodeRouter.get("/generate", async (req, res) => { | ||
const accessToken = sign( | ||
"" + new Date().getTime(), | ||
process.env.SECRET as string, | ||
{ algorithm: "HS256", expiresIn: 1000 * 60 * 5 }, | ||
); | ||
const payload = encodeURI( | ||
`${process.env.CLIENT_BASE_URL}?access_token=${accessToken}`, | ||
); | ||
const dataUrl = await generateQRCode(payload); | ||
res.status(200).send({ dataUrl }); | ||
}); | ||
|
||
// generate qr code with access token (5 min expiry time) encoded in url parameters | ||
// landing page on website where key is decoded, validates against api, then adds entry to spreadsheet | ||
|
||
export { qrCodeRouter }; |
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
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
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 |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import { QRCodeData } from "../types"; | ||
import QRCode from "qrcode"; | ||
import Jimp from "jimp"; | ||
import jsqr, { QRCode as QRC } from "jsqr"; | ||
|
||
export const generateQRCode = async (userData: QRCodeData) => { | ||
const payload = encodeURI( | ||
`${process.env.ADMIN_CLIENT_BASE_URL}?email=${userData.email}&name=${userData.name}&id=${userData.id}`, | ||
); | ||
export const generateQRCode = async <P extends string>(payload: P) => { | ||
return await QRCode.toDataURL(payload, { width: 250 }); | ||
}; | ||
|
||
export const decodeQRCodeDataUrl = async ( | ||
dataUrl: string, | ||
): Promise<string | void> => { | ||
try { | ||
const buffer = Buffer.from( | ||
dataUrl.replace(/^data:image\/[a-z]+;base64,/, ""), | ||
"base64", | ||
); | ||
const image = await Jimp.read(buffer); | ||
const decoded = jsqr( | ||
new Uint8ClampedArray(image.bitmap.data), | ||
image.bitmap.width, | ||
image.bitmap.height, | ||
) as QRC; | ||
|
||
return decoded.data; | ||
} catch (err) { | ||
console.error(Error("Error: QR Decoding failed")); | ||
console.error(err); | ||
} | ||
}; |
Oops, something went wrong.