-// Logic to login, or create / update a user (and also logout)
<template lang="pug">
div
input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
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: "",
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);
},
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: "/",
},
"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: "/",
},
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 = {
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)
{
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,
+ });
});
});
});