3 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
6 label.modal-close(for="modalUser")
8 form#userForm(@submit.prevent="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(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 submitMessage: function() {
61 displayInfo: function() {
62 return (this.infoMsg.length > 0 ? "block" : "none");
65 return this.user.id > 0 ? "Update" : this.logStage;
69 trySetEnterTime: function(event) {
70 if (!!event.target.checked)
71 this.enterTime = Date.now();
73 toggleStage: function() {
74 // Loop login <--> register (update is for logged-in users)
75 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
88 ajaxMethod: function() {
99 infoMessage: function() {
103 return "Connection token sent. Check your emails!";
105 return "Registration complete! Please check your emails.";
107 return "Modifications applied!";
110 onSubmit: function() {
111 // Basic anti-bot strategy:
112 const exitTime = Date.now();
113 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
114 return; //silently return, in (curious) case of it was legitimate
115 let error = undefined;
116 if (this.stage == 'Login')
118 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
119 error = checkNameEmail({[type]: this.nameOrEmail});
122 error = checkNameEmail(this.user);
125 this.infoMsg = "Processing... Please wait";
126 ajax(this.ajaxUrl(), this.ajaxMethod(),
127 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
129 this.infoMsg = this.infoMessage();
130 if (this.stage != "Update")
131 this.nameOrEmail = "";
134 document.getElementById("modalUser").checked = false;
143 doLogout: function() {
150 this.user.email = "";
151 this.user.notify = false;
152 delete localStorage["myid"];
153 delete localStorage["myname"];