3 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
4 div(role="dialog" data-checkbox="modalUser")
6 label.modal-close(for="modalUser")
8 form#userForm(@submit.prevent="onSubmit()" @keyup.enter="onSubmit")
9 div(v-show="stage!='Login'")
11 label(for="username") Name
12 input#username(type="text" v-model="user.name")
14 label(for="useremail") Email
15 input#useremail(type="email" v-model="user.email")
17 label(for="notifyNew") Notify new moves & games
18 input#notifyNew(type="checkbox" v-model="user.notify")
19 div(v-show="stage=='Login'")
21 label(for="nameOrEmail") Name or Email
22 input#nameOrEmail(type="text" v-model="nameOrEmail")
24 button#submit(type="button" @click="onSubmit()")
25 span {{ submitMessage }}
27 button(v-if="stage!='Update'" @click="toggleStage()")
28 span {{ stage=="Login" ? "Register" : "Login" }}
29 button#logoutBtn(v-else @click="doLogout()")
31 #dialog(:style="{display: displayInfo}") {{ infoMsg }}
35 import { store } from "@/store";
36 import { checkNameEmail } from "@/data/userCheck";
37 import { ajax } from "@/utils/ajax";
39 name: 'my-upsert-user',
42 user: store.state.user,
43 nameOrEmail: "", //for login
44 logStage: "Login", //or Register
46 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
50 nameOrEmail: function(newValue) {
51 if (newValue.indexOf('@') >= 0)
53 this.user.email = newValue;
58 this.user.name = newValue;
64 submitMessage: function() {
75 displayInfo: function() {
76 return (this.infoMsg.length > 0 ? "block" : "none");
79 return this.user.id > 0 ? "Update" : this.logStage;
83 trySetEnterTime: function(event) {
84 if (!!event.target.checked)
85 this.enterTime = Date.now();
87 toggleStage: function() {
88 // Loop login <--> register (update is for logged-in users)
89 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
102 ajaxMethod: function() {
113 infoMessage: function() {
117 return "Connection token sent. Check your emails!";
119 return "Registration complete! Please check your emails.";
121 return "Modifications applied!";
124 onSubmit: function() {
125 // Basic anti-bot strategy:
126 const exitTime = Date.now();
127 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
128 return; //silently return, in (curious) case of it was legitimate
129 let error = undefined;
130 if (this.stage == 'Login')
132 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
133 error = checkNameEmail({[type]: this.nameOrEmail});
136 error = checkNameEmail(this.user);
139 this.infoMsg = "Processing... Please wait";
140 ajax(this.ajaxUrl(), this.ajaxMethod(),
141 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
143 this.infoMsg = this.infoMessage();
144 if (this.stage != "Update")
145 this.nameOrEmail = "";
148 document.getElementById("modalUser").checked = false;
157 doLogout: function() {
158 let logoutBtn = document.getElementById("logoutBtn");
159 logoutBtn.disabled = true;
160 // NOTE: this local cleaning would logically happen when we're sure
161 // that token is erased. But in the case a user clear the cookies,
162 // it would lead to situations where he cannot ("locally") log out.
163 // At worst, if token deletion fails the user can erase cookie manually.
166 this.user.email = "";
167 this.user.notify = false;
168 localStorage.removeItem("myid");
169 localStorage.removeItem("myname");
170 ajax("/logout", "GET", () => {
171 logoutBtn.disabled = false; //for symmetry, but not very useful...
172 document.getElementById("modalUser").checked = false;
173 // this.$router.push("/") will fail if logout from Hall, so:
174 document.location.reload(true);