Quellcode durchsuchen

chor!: redis is now mandotary to start the app

poeti8 vor 6 Jahren
Ursprung
Commit
c3199ab4bc
3 geänderte Dateien mit 40 neuen und 44 gelöschten Zeilen
  1. 2 3
      .example.env
  2. 26 21
      server/controllers/authController.ts
  3. 12 20
      server/redis.ts

+ 2 - 3
.example.env

@@ -17,7 +17,6 @@ NEO4J_DB_USERNAME=neo4j
 NEO4J_DB_PASSWORD=BjEphmupAf1D5pDD
 
 # Redis host and port
-REDIS_DISABLED=false
 REDIS_HOST="127.0.0.1"
 REDIS_PORT=6379
 REDIS_PASSWORD=
@@ -33,7 +32,7 @@ NON_USER_COOLDOWN=0
 DEFAULT_MAX_STATS_PER_LINK=5000
 
 # Use HTTPS for links with custom domain
-CUSTOM_DOMAIN_USE_HTTPS=false 
+CUSTOM_DOMAIN_USE_HTTPS=false
 
 # A passphrase to encrypt JWT. Use a long and secure key.
 JWT_SECRET=securekey
@@ -74,4 +73,4 @@ MAIL_PASSWORD=
 REPORT_MAIL=
 
 # Support email to show on the app
-CONTACT_EMAIL=
+CONTACT_EMAIL=

+ 26 - 21
server/controllers/authController.ts

@@ -136,7 +136,6 @@ export const signup: Handler = async (req, res) => {
   if (email.length > 255) {
     return res.status(400).json({ error: "Maximum email length is 255." });
   }
-
   const user = await getUser(email);
 
   if (user && user.verified) {
@@ -145,29 +144,35 @@ export const signup: Handler = async (req, res) => {
 
   const newUser = await createUser(email, password, user);
 
-  const mail = await transporter.sendMail({
-    from: process.env.MAIL_FROM || process.env.MAIL_USER,
-    to: newUser.email,
-    subject: "Verify your account",
-    text: verifyMailText.replace(
-      /{{verification}}/gim,
-      newUser.verification_token
-    ),
-    html: verifyEmailTemplate.replace(
-      /{{verification}}/gim,
-      newUser.verification_token
-    )
-  });
+  try {
+
+
+    const mail = await transporter.sendMail({
+      from: process.env.MAIL_FROM || process.env.MAIL_USER,
+      to: newUser.email,
+      subject: "Verify your account",
+      text: verifyMailText.replace(
+        /{{verification}}/gim,
+        newUser.verification_token
+      ),
+      html: verifyEmailTemplate.replace(
+        /{{verification}}/gim,
+        newUser.verification_token
+      )
+    });
+
+    if (mail.accepted.length) {
+      return res
+        .status(201)
+        .json({ email, message: "Verification email has been sent." });
+    }
 
-  if (mail.accepted.length) {
     return res
-      .status(201)
-      .json({ email, message: "Verification email has been sent." });
+      .status(400)
+      .json({ error: "Couldn't send verification email. Try again." });
+  } catch (error) {
+    console.log({ error });
   }
-
-  return res
-    .status(400)
-    .json({ error: "Couldn't send verification email. Try again." });
 };
 
 export const login: Handler = (req, res) => {

+ 12 - 20
server/redis.ts

@@ -1,31 +1,23 @@
 import { promisify } from "util";
 import redis from "redis";
 
-const disabled = process.env.REDIS_DISABLED === "true";
+const client = redis.createClient({
+  host: process.env.REDIS_HOST || "127.0.0.1",
+  port: Number(process.env.REDIS_PORT) || 6379,
+  ...(process.env.REDIS_PASSWORD && { password: process.env.REDIS_PASSWORD })
+});
 
-const client =
-  !disabled &&
-  redis.createClient({
-    host: process.env.REDIS_HOST || "127.0.0.1",
-    port: Number(process.env.REDIS_PORT) || 6379,
-    ...(process.env.REDIS_PASSWORD && { password: process.env.REDIS_PASSWORD })
-  });
-
-const defaultResolver: () => Promise<null> = () => Promise.resolve(null);
-
-export const get: (key: string) => Promise<any> = disabled
-  ? defaultResolver
-  : promisify(client.get).bind(client);
+export const get: (key: string) => Promise<any> = promisify(client.get).bind(
+  client
+);
 
 export const set: (
   key: string,
   value: string,
   ex?: string,
   exValue?: number
-) => Promise<any> = disabled
-  ? defaultResolver
-  : promisify(client.set).bind(client);
+) => Promise<any> = promisify(client.set).bind(client);
 
-export const del: (key: string) => Promise<any> = disabled
-  ? defaultResolver
-  : promisify(client.del).bind(client);
+export const del: (key: string) => Promise<any> = promisify(client.del).bind(
+  client
+);