Attempt to fix authenticate + local user data
authorBenjamin Auder <benjamin.auder@somewhere>
Mon, 25 Mar 2019 16:41:09 +0000 (17:41 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Mon, 25 Mar 2019 16:41:09 +0000 (17:41 +0100)
client/src/components/UpsertUser.vue
client/src/router.js
client/src/store.js
server/routes/users.js

index 7b92cf9..46f3bfe 100644 (file)
@@ -1,4 +1,3 @@
-// Logic to login, or create / update a user (and also logout)
 <template lang="pug">
 div
   input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
@@ -40,7 +39,7 @@ export default {
   name: 'my-upsert-user',
   data: function() {
     return {
-      user: store.state.user,
+      user: Object.assign({}, store.state.user),
       nameOrEmail: "", //for login
       stage: (store.state.user.id > 0 ? "Update" : "Login"), //TODO?
       infoMsg: "",
@@ -126,20 +125,9 @@ export default {
         res => {
           this.infoMsg = this.infoMessage();
           if (this.stage != "Update")
-          {
             this.nameOrEmail = "";
-            this.user["email"] = "";
-            // Update global object
-            this.user["name"] = res.name;
-            this.user["id"] = res.id;
-            // Store our identifiers in local storage (by little anticipation...)
-            localStorage["myid"] = res.id;
-            localStorage["myname"] = res.name;
-          }
           setTimeout(() => {
             this.infoMsg = "";
-            if (this.stage == "Register")
-              this.stage = "Login";
             document.getElementById("modalUser").checked = false;
           }, 2000);
         },
index 2030397..89bdf73 100644 (file)
@@ -32,16 +32,21 @@ export default new Router({
       path: "/authenticate/:token",
       name: "authenticate",
       beforeEnter: (to, from, next) => {
+        console.log("ajax call authenticate");
         ajax(
           "/authenticate",
           "GET",
           {token: to.params["token"]},
           (res) => {
+            console.log(res);
             store.state.user.id = res.id;
             store.state.user.name = res.name;
+            store.state.user.email = res.email;
+            store.state.user.notify = res.notify;
+            // NOTE: mysid isn't cleared (required for potential game continuation)
+            next();
           }
         );
-        next();
       },
       redirect: "/",
     },
@@ -54,10 +59,12 @@ export default new Router({
           "GET",
           () => {
             store.state.user.id = 0;
-            store.state.user.name = ""; //TODO: localStorage myId myname mysid ?
+            store.state.user.name = "";
+            store.state.user.email = "";
+            store.state.user.notify = false;
+            next();
           }
         );
-        next();
       },
       redirect: "/",
     },
index e9bbf65..935005a 100644 (file)
@@ -15,10 +15,21 @@ export const store =
   initialize() {
     ajax("/variants", "GET", res => { this.state.variants = res.variantArray; });
     this.state.user = {
-      id: localStorage["myuid"] || 0,
-      name: localStorage["myname"] || "", //"anonymous"
+      id: 0, //unknown yet
+      name: "", //"anonymous"
+      email: "", //unknown yet
+      notify: false, //email notifications
       sid: localStorage["mysid"] || getRandString(),
     };
+    ajax("/whoami", "GET", res => {
+      if (res.id > 0)
+      {
+        this.state.user.id = res.id;
+        this.state.user.name = res.name;
+        this.state.user.email = res.email;
+        this.state.user.notify = res.notify;
+      }
+    });
     this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + this.state.user.sid);
     // Settings initialized with values from localStorage
     this.state.settings = {
index ebbfa1e..4b142d7 100644 (file)
@@ -7,6 +7,27 @@ var genToken = require("../utils/tokenGenerator");
 var access = require("../utils/access");
 var params = require("../config/parameters");
 
+router.get("/whoami", access.ajax, (req,res) => {
+  const callback = (user) => {
+    return res.json({
+      name: user.name,
+      email: user.email,
+      id: user.id,
+      notify: user.notify,
+    });
+  };
+  const anonymous = {name:"", email:"", id:0, notify:false};
+  console.log(req.cookies); //TODO: cookie not found after authenticate ?
+       if (!req.cookies.token)
+    return callback(anonymous);
+  UserModel.getOne("sessionToken", req.cookies.token, function(err, user) {
+    if (!!err || !user)
+      callback(anonymous);
+    else (!!user)
+      callback(user);
+  });
+});
+
 // to: object user (to who we send an email)
 function setAndSendLoginToken(subject, to, res)
 {
@@ -71,12 +92,17 @@ router.get('/authenticate', access.unlogged, access.ajax, (req,res) => {
                                if (!!err)
                                        return res.json({errmsg: err.toString()});
                                // Set cookie
-                               res.cookie("token", token, {
+        res.cookie("token", token, {
                                        httpOnly: true,
                                        secure: !!params.siteURL.match(/^https/),
                                        maxAge: params.cookieExpire,
                                });
-                               res.json({name:user.name, id:user.id});
+                               res.json({
+          id: user.id,
+          name: user.name,
+          email: user.email,
+          notify: user.notify,
+        });
                        });
                });
        });