Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service worker registration #80

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import bcrypt from "bcrypt";
const PORT = process.env.PORT || 3000;
const __dirname = process.cwd();
const ACTIVE_CODES = new Set();
if (!fs.existsSync("./memory.txt")) {
fs.writeFileSync("./memory.txt", "", "utf-8");
}
let TOKENS = fs
.readFileSync("./memory.txt", "utf-8")
.trim()
Expand Down Expand Up @@ -43,9 +46,9 @@ app.use(
// Verification
app.patch("/generate-otp", async (req, res) => {
if (
config.sendgrid_verification == true ||
config.discord_verification == true ||
config.smtp_verificaton == true
config.sendgrid_verification ||
config.discord_verification ||
config.smtp_verificaton
) {
const OTP = generateCode();
ACTIVE_CODES.add(OTP);
Expand All @@ -64,7 +67,7 @@ app.patch("/generate-otp", async (req, res) => {
(this message is automated)`
};

if (config.sendgrid_verification == true) {
if (config.sendgrid_verification) {
sgMail.setApiKey(config.sendgrid_options.api_key);

email.to = config.sendgrid_options.to_email;
Expand All @@ -76,7 +79,7 @@ app.patch("/generate-otp", async (req, res) => {
}
}

if (config.smtp_verification == true) {
if (config.smtp_verification) {
const smtpMailerAgent = nodemailer.createTransport(config.smtp_options);

email.to = config.smtp_options.to_email;
Expand All @@ -88,7 +91,7 @@ app.patch("/generate-otp", async (req, res) => {
}
}

if (config.discord_verification == true) {
if (config.discord_verification) {
try {
await fetch(config.webhook_url, {
method: "POST",
Expand Down Expand Up @@ -117,9 +120,9 @@ function generateCode() {

app.post("/validate-otp", (req, res) => {
if (
config.sendgrid_verification == true ||
config.discord_verification == true ||
config.smtp_verificaton == true
config.sendgrid_verification ||
config.discord_verification ||
config.smtp_verificaton
) {
const OTP = req.body.otp;

Expand Down Expand Up @@ -163,9 +166,9 @@ app.use(express.static(path.join(__dirname, "public")));
// Login route
app.get("/login", (req, res) => {
if (
config.sendgrid_verification == true ||
config.discord_verification == true ||
config.smtp_verificaton == true
config.sendgrid_verification ||
config.discord_verification ||
config.smtp_verificaton
) {
res.sendFile(path.join(__dirname, "src", "unv.html"));
} else {
Expand All @@ -175,12 +178,17 @@ app.get("/login", (req, res) => {

// General Routes
app.use((req, res, next) => {
const verification = req.cookies["validation"];
if (!verification || !validateToken(verification)) {
res.redirect("/login");
} else {
next();
if (
config.sendgrid_verification ||
config.discord_verification ||
config.smtp_verificaton
) {
const verification = req.cookies["validation"];
if (!verification || !validateToken(verification)) {
return res.redirect("/login");
}
}
next();
});

app.get("/", (req, res) => {
Expand Down Expand Up @@ -226,7 +234,6 @@ function hash(token) {
}

function validateToken(verification) {
console.log(verification);
const [id, token] = verification.split(":");
const tokenData = TOKENS.find((token) => token.id == id);

Expand Down
2 changes: 2 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
importScripts("./uv/uv.bundle.js");
importScripts("./uv/uv.config.js");
importScripts("./uv/uv.sw.js");
importScripts("./osana/osana.worker.js");

Expand Down