this.user.name = "";
this.user.email = "";
this.user.notify = false;
+ delete localStorage["myid"];
+ delete localStorage["myname"];
}
);
},
// URL of the server (leave blank for 1-server case)
serverUrl: "http://localhost:3000",
+
+ // true if the server is at a different address
+ cors: false,
};
export default Parameters;
"GET",
{token: to.params["token"]},
(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)
+ if (!res.errmsg) //if not already logged in
+ {
+ store.state.user.id = res.id;
+ store.state.user.name = res.name;
+ store.state.user.email = res.email;
+ store.state.user.notify = res.notify;
+ localStorage["myname"] = res.name;
+ localStorage["myid"] = res.id;
+ }
next();
}
);
},
initialize() {
ajax("/variants", "GET", res => { this.state.variants = res.variantArray; });
+ let mysid = localStorage["mysid"];
+ if (!mysid)
+ {
+ mysid = getRandString();
+ localStorage["mysid"] = mysid; //done only once (unless user clear browser data)
+ }
this.state.user = {
- id: 0, //unknown yet
- name: "", //"anonymous"
+ id: localStorage["myid"] || 0,
+ name: localStorage["myname"] || "", //"" for "anonymous"
email: "", //unknown yet
notify: false, //email notifications
- sid: localStorage["mysid"] || getRandString(),
+ sid: mysid,
};
- ajax("/whoami", "GET", res => {
- if (res.id > 0)
- {
- this.state.user.id = res.id;
- this.state.user.name = res.name;
+ if (this.state.user.id > 0)
+ {
+ fetch(params.serverUrl + "/whoami", {
+ method: "GET",
+ credentials: params.cors ? "include" : "omit",
+ }).then((res) => {
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 = {
xhr.open(method, params.serverUrl + url, true);
xhr.setRequestHeader('X-Requested-With', "XMLHttpRequest");
// Next line to allow cross-domain cookies in dev mode (TODO: if...)
- xhr.withCredentials = true;
+ if (params.cors)
+ xhr.withCredentials = true;
if (["POST","PUT"].includes(method))
{
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
fen: "",
vid: 0,
nbPlayers: 0,
- to: ["", "", ""], //name of challenged players
+ to: ["", "", ""], //name(s) of challenged player(s)
timeControl: "", //"2m+2s" ...etc
},
};
return playerList;
},
},
- watch: {
- // Watch event "user infos retrieved" (through /whoami)
- "st.user.id": function(newId) {
- if (newId > 0) //should always be the case
- {
- // Ask server for current corr games (all but mines)
- // ajax(
- // "/games",
- // "GET",
- // {excluded: this.st.user.id},
- // response => {
- // this.games = this.games.concat(response.games);
- // }
- // );
- // Also ask for corr challenges (open + sent to me)
- ajax(
- "/challenges",
- "GET",
- {uid: this.st.user.id},
- response => {
- console.log(response.challenges);
- // TODO: post-treatment on challenges ?
- this.challenges = this.challenges.concat(response.challenges);
- }
- );
- }
- },
- },
created: function() {
// Always add myself to players' list
this.players.push(this.st.user);
+ if (this.st.user.id > 0)
+ {
+ // Ask server for current corr games (all but mines)
+// ajax(
+// "/games",
+// "GET",
+// {excluded: this.st.user.id},
+// response => {
+// this.games = this.games.concat(response.games);
+// }
+// );
+ // Also ask for corr challenges (open + sent to me)
+ ajax(
+ "/challenges",
+ "GET",
+ {uid: this.st.user.id},
+ response => {
+ console.log(response.challenges);
+ // TODO: post-treatment on challenges ?
+ this.challenges = this.challenges.concat(response.challenges);
+ }
+ );
+ }
// 0.1] Ask server for room composition:
const socketOpenListener = () => {
this.st.conn.send(JSON.stringify({code:"pollclients"}));
}
case "deletechallenge":
{
- console.log("receive delete");
ArrayFun.remove(this.challenges, c => c.id == data.cid);
break;
}
if (!!error)
return alert(error);
const ctype = this.classifyObject(this.newchallenge);
- const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers);
+ const cto = this.newchallenge.to.slice(0, this.newchallenge.nbPlayers - 1);
// NOTE: "from" information is not required here
let chall =
{
// * - prepare and start new game (if challenge is full after acceptation)
// * --> include challenge ID (so that opponents can delete the challenge too)
clickChallenge: function(c) {
-
+
+ console.log("click challenge");
console.log(c);
-
+
if (!!c.accepted)
{
this.st.conn.send(JSON.stringify({code: "withdrawchallenge",
else if (c.from.sid == this.st.user.sid
|| (this.st.user.id > 0 && c.from.id == this.st.user.id))
{
- console.log("send delete");
// It's my challenge: cancel it
this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
ArrayFun.remove(this.challenges, ch => ch.id == c.id);
localStorage["increment"] = tc.increment;
localStorage["started"] = JSON.stringify(
[...Array(gameInfo.players.length)].fill(false));
- localStorage["mysid"] = this.st.user.sid;
localStorage["vname"] = this.getVname(gameInfo.vid);
localStorage["fenInit"] = gameInfo.fen;
localStorage["players"] = JSON.stringify(gameInfo.players);
router.get('/authenticate', access.unlogged, access.ajax, (req,res) => {
UserModel.getOne("loginToken", req.query.token, (err,user) => {
access.checkRequest(res, err, user, "Invalid token", () => {
- // If token older than params.tokenExpire, do nothing
+ // If token older than params.tokenExpire, do nothing
if (Date.now() > user.loginTime + params.token.expire)
return res.json({errmsg: "Token expired"});
// Generate session token (if not exists) + destroy login token
clients[obj.target].send(
JSON.stringify({code:"refusechallenge", cid:obj.cid, from:sid}));
break;
+ case "deletechallenge":
+ clients[obj.target].send(
+ JSON.stringify({code:"deletechallenge", cid:obj.cid, from:sid}));
+ break;
case "newgame":
clients[obj.target].send(JSON.stringify(
{code:"newgame", gameInfo:obj.gameInfo, cid:obj.cid}));
logged: function(req, res, next) {
const callback = () => {
if (!loggedIn)
- return res.redirect("/");
+ return res.json({errmsg: "Not logged in"});
next();
};
let loggedIn = undefined;
// Just a quick heuristic, which should be enough
const loggedIn = !!req.cookies.token;
if (loggedIn)
- return res.redirect("/");
+ return res.json({errmsg: "Already logged in"});
next();
},