From: Benjamin Auder <benjamin.auder@somewhere> Date: Mon, 25 Mar 2019 16:41:09 +0000 (+0100) Subject: Attempt to fix authenticate + local user data X-Git-Url: https://git.auder.net/js/doc/%7B%7B%20path('fos_user_registration_register')%20%7D%7D?a=commitdiff_plain;h=a7f9f050e44080e8caf888e3b230660abffa2400;p=vchess.git Attempt to fix authenticate + local user data --- diff --git a/client/src/components/UpsertUser.vue b/client/src/components/UpsertUser.vue index 7b92cf9a..46f3bfe8 100644 --- a/client/src/components/UpsertUser.vue +++ b/client/src/components/UpsertUser.vue @@ -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); }, diff --git a/client/src/router.js b/client/src/router.js index 2030397a..89bdf738 100644 --- a/client/src/router.js +++ b/client/src/router.js @@ -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: "/", }, diff --git a/client/src/store.js b/client/src/store.js index e9bbf653..935005ab 100644 --- a/client/src/store.js +++ b/client/src/store.js @@ -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 = { diff --git a/server/routes/users.js b/server/routes/users.js index ebbfa1e6..4b142d71 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -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, + }); }); }); });