1 // Logic to login, or create / update a user (and also logout)
4 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
7 label.modal-close(for="modalUser")
9 form#userForm(@submit.prevent="onSubmit()")
10 div(v-show="stage!='Login'")
12 label(for="username") Name
13 input#username(type="text" v-model="user.name")
15 label(for="useremail") Email
16 input#useremail(type="email" v-model="user.email")
18 label(for="notifyNew") Notify new moves & games
19 input#notifyNew(type="checkbox" v-model="user.notify")
20 div(v-show="stage=='Login'")
22 label(for="nameOrEmail") Name or Email
23 input#nameOrEmail(type="text" v-model="nameOrEmail")
25 button#submit(type="button" @click="onSubmit()")
26 span {{ submitMessage }}
28 button(v-if="stage!='Update'" @click="toggleStage()")
29 span {{ stage=="Login" ? "Register" : "Login" }}
30 button(v-else onClick="location.replace('/logout')")
32 #dialog(:style="{display: displayInfo}") {{ infoMsg }}
36 import { store } from "@/store";
37 import { checkNameEmail } from "@/data/userCheck";
38 import { ajax } from "@/utils/ajax";
40 name: 'my-upsert-user',
43 user: store.state.user,
44 nameOrEmail: "", //for login
45 stage: (store.state.user.id > 0 ? "Update" : "Login"), //TODO?
47 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
51 submitMessage: function() {
62 displayInfo: function() {
63 return (this.infoMsg.length > 0 ? "block" : "none");
67 trySetEnterTime: function(event) {
68 if (!!event.target.checked)
69 this.enterTime = Date.now();
71 toggleStage: function() {
72 // Loop login <--> register (update is for logged-in users)
73 this.stage = (this.stage == "Login" ? "Register" : "Login");
86 ajaxMethod: function() {
97 infoMessage: function() {
101 return "Connection token sent. Check your emails!";
103 return "Registration complete! Please check your emails.";
105 return "Modifications applied!";
108 onSubmit: function() {
109 // Basic anti-bot strategy:
110 const exitTime = Date.now();
111 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
112 return; //silently return, in (curious) case of it was legitimate
113 let error = undefined;
114 if (this.stage == 'Login')
116 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
117 error = checkNameEmail({[type]: this.nameOrEmail});
120 error = checkNameEmail(this.user);
123 this.infoMsg = "Processing... Please wait";
124 ajax(this.ajaxUrl(), this.ajaxMethod(),
125 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
127 this.infoMsg = this.infoMessage();
128 if (this.stage != "Update")
130 this.nameOrEmail = "";
131 this.user["email"] = "";
132 // Update global object
133 this.user["name"] = res.name;
134 this.user["id"] = res.id;
135 // Store our identifiers in local storage (by little anticipation...)
136 localStorage["myid"] = res.id;
137 localStorage["myname"] = res.name;
141 if (this.stage == "Register")
142 this.stage = "Login";
143 document.getElementById("modalUser").checked = false;