Skip to content

Commit

Permalink
Show QRCode for login
Browse files Browse the repository at this point in the history
  • Loading branch information
hsc-nue committed Sep 17, 2023
1 parent 64cf25b commit c408c4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
17 changes: 14 additions & 3 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
require("dotenv").config();
import nodemailer from "nodemailer";
const bodyParser = require("body-parser");
import jwt from "jsonwebtoken";
var QRCode = require("qrcode");

const app = express();

Expand All @@ -36,9 +38,18 @@ mongoose

// routes
app.get("*", checkUser);
app.get("/", requireAuth, requireUserVerified, (req: any, res: any) =>
res.render("home", { user: res.locals.user })
);

const home_get = async (req: any, res: any) => {
const user = res.locals.user;
const userToken = jwt.sign(
{ id: user.id, email: user.email },
process.env.JWT_SECRET!,
{ expiresIn: "5y" } // TODO: check in "y" valid
);
const qrCodeDataUrl = await QRCode.toDataURL(userToken);
res.render("home", { user, qrCodeDataUrl });
};
app.get("/", requireAuth, requireUserVerified, home_get);

app.use("/score", score);
app.use("/user", user);
Expand Down
20 changes: 19 additions & 1 deletion dist/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -12,6 +21,8 @@ const cookieParser = require("cookie-parser");
const { requireAuth, checkUser, requireAdmin, requireUserVerified, } = require("./middleware/authMiddleware");
require("dotenv").config();
const bodyParser = require("body-parser");
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
var QRCode = require("qrcode");
const app = express();
// middleware
app.use(express.static("public"));
Expand All @@ -30,7 +41,14 @@ mongoose_1.default
.catch((err) => console.log(err));
// routes
app.get("*", checkUser);
app.get("/", requireAuth, requireUserVerified, (req, res) => res.render("home", { user: res.locals.user }));
const home_get = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const user = res.locals.user;
const userToken = jsonwebtoken_1.default.sign({ id: user.id, email: user.email }, process.env.JWT_SECRET, { expiresIn: "5y" } // TODO: check in "y" valid
);
const qrCodeDataUrl = yield QRCode.toDataURL(userToken);
res.render("home", { user, qrCodeDataUrl });
});
app.get("/", requireAuth, requireUserVerified, home_get);
app.use("/score", score);
app.use("/user", user);
app.use(router);
Expand Down
30 changes: 18 additions & 12 deletions views/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@

<ul class="w3-ul w3-border">
<% if (user.isAdmin) { %>
<h3>Noten</h3>
<li><a href="/score/register" class="w3-button w3-black">Leihnoten registrieren</a></li>
<li><a href="/score/checkout" class="w3-button w3-black">Noten Ausleihe</a></li>
<li><a href="/score/checkin" class="w3-button w3-black">Noten Rückgabe</a></li>
<li><a href="/score/checkouts" class="w3-button w3-black">Ausleihen anzeigen</a></li>
<!-- <li><a href="/list-score" class="w3-button w3-black">Leihnoten Liste</a></li> -->
<h3>Benutzer</h3>
<li><a href="/signup-user" class="w3-button w3-black">Benutzer anlegen</a></li>
<li><a href="/score/users" class="w3-button w3-black">Benutzer Liste</a></li>
<% } else { %>
<li><a href="/user/checkouts" class="w3-button w3-black">Ausleihen anzeigen</a></li>
<% } %>
<h3>Noten</h3>
<li><a href="/score/register" class="w3-button w3-black">Leihnoten registrieren</a></li>
<li><a href="/score/checkout" class="w3-button w3-black">Noten Ausleihe</a></li>
<li><a href="/score/checkin" class="w3-button w3-black">Noten Rückgabe</a></li>
<li><a href="/score/checkouts" class="w3-button w3-black">Ausleihen anzeigen</a></li>
<h3>Benutzer</h3>
<li><a href="/signup-user" class="w3-button w3-black">Benutzer anlegen</a></li>
<li><a href="/score/users" class="w3-button w3-black">Benutzer Liste</a></li>
<% } else { %>
<li><a href="/user/checkouts" class="w3-button w3-black">Ausleihen anzeigen</a></li>
<li><a href="#" onClick="document.querySelector('.divQrCode').style='display: block'" class="w3-button w3-black">QRCode für Login anzeigen</a></li>
<% } %>
</ul>

<% if (!user.isAdmin) { %>
<div class="divQrCode" style="display: none">
<img src="<%= qrCodeDataUrl %>" style="width:100%;max-width:300px">
</div>
<% } %>

<%- include('partials/footer'); -%>

0 comments on commit c408c4f

Please sign in to comment.