-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
33 lines (28 loc) · 801 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import express from "express";
import speakeasy from "speakeasy";
import qrcode from "qrcode";
import fs from "fs";
const secret = speakeasy.generateSecret({ name: "MyProduct" });
const app = express();
qrcode.toDataURL(secret.otpauth_url, (err, data) => {
if (err) throw err;
app.get("/", (req, res) => {
const html = fs.readFileSync("./index.html", "utf8");
res.send(html.replace("QR_CODE_DATA_URL", data));
});
});
app.get("/verify", (req, res) => {
const token = req.query.token;
const verified = speakeasy.totp.verify({
secret: secret.base32,
encoding: "base32",
token: token,
});
res.send({
verified,
message: verified ? "Token is valid" : "Token is invalid",
});
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});