Browse Source

Fixed register invalid token bug

liontix 11 months ago
parent
commit
586264ae2e
3 changed files with 5 additions and 5 deletions
  1. 1 0
      public/javascripts/appointments.js
  2. 1 2
      public/javascripts/auth.js
  3. 3 3
      routes/auth.js

+ 1 - 0
public/javascripts/appointments.js

@@ -104,6 +104,7 @@ async function fetchAppointments() {
 
     const js = await parseOrRedirect(response);
 
+    if (js.length === 0) return;
     renderAppointments(js);
 }
 

+ 1 - 2
public/javascripts/auth.js

@@ -19,8 +19,6 @@ window.addEventListener("load", function () {
 });
 
 async function sendAuthRequest(type) {
-    console.log(emailInput.value);
-    console.log(passwordInput.value);
     const response = await fetchRegular(
         "/api/auth/" + type,
         {
@@ -39,6 +37,7 @@ async function sendAuthRequest(type) {
     const response_json = await response.json();
 
     if (response.status === 200) {
+        console.log("success")
         localStorage.setItem("token", response_json["token"]);
         window.location.replace("/html/appointments.html");
     } else {

+ 3 - 3
routes/auth.js

@@ -46,7 +46,7 @@ router.post('/login', function (req, res) {
             if (!user) {
                 return res.status(422).json({ message: 'user not found' });
             }
-
+            console.log(user);
             bcrypt.compare(password, user.password, function (err, result) {
                 if (result) {
                     res.json({ token: generateToken(user) });
@@ -80,14 +80,14 @@ router.post('/register', async function (req, res) {
         if (existingUser) {
             return res.status(403).json({ message: 'user already exists' });
         }
-
         // Hash the password
         bcrypt.hash(password, saltRounds, async function (err, hash) {
             if (err) {
                 return res.status(500).json({ error: err.message });
             } else if (hash) {
                 // Insert user into database and generate token
-                const user = await User.collection.insertOne({ email: email, password: hash });
+                await User.collection.insertOne({ email: email, password: hash });
+                const user = await User.findOne({ email: email });
                 return res.json({ token: generateToken(user) });
             }
         });