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</label>
16 <input id="useremail" type="email" v-model="user.email"/>
18 <label for="notifyNew">Notify new moves & games</label>
19 <input id="notifyNew" type="checkbox" v-model="user.notify"/>
20 div(v-show="stage=='Login'")
22 <label for="nameOrEmail">Name or Email</label>
23 <input id="nameOrEmail" type="text" v-model="nameOrEmail"/>
25 button#submit(@click="onSubmit()")
26 span {{ submitMessage }}
28 button(v-if="stage!='Update'" @click="toggleStage()")
29 span {{ stage=="Login" ? "Register" : "Login" }}
30 button(v-if="stage=='Update'" onClick="location.replace('/logout')")
32 #dialog(:style="{display: displayInfo}") {{ infoMsg }}
36 import { store } from "@/store";
38 name: 'my-upsert-user',
41 user: store.state.user, //initialized with global user object
42 nameOrEmail: "", //for login
43 stage: (!store.state.user.id ? "Login" : "Update"),
45 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
49 submitMessage: function() {
60 displayInfo: function() {
61 return (this.infoMsg.length > 0 ? "block" : "none");
65 trySetEnterTime: function(event) {
66 if (!!event.target.checked)
67 this.enterTime = Date.now();
69 toggleStage: function() {
70 // Loop login <--> register (update is for logged-in users)
71 this.stage = (this.stage == "Login" ? "Register" : "Login");
84 ajaxMethod: function() {
95 infoMessage: function() {
99 return "Connection token sent. Check your emails!";
101 return "Registration complete! Please check your emails.";
103 return "Modifications applied!";
106 onSubmit: function() {
107 // Basic anti-bot strategy:
108 const exitTime = Date.now();
109 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
110 return; //silently return, in (curious) case of it was legitimate
111 let error = undefined;
112 if (this.stage == 'Login')
114 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
115 error = checkNameEmail({[type]: this.nameOrEmail});
118 error = checkNameEmail(this.user);
121 this.infoMsg = "Processing... Please wait";
122 ajax(this.ajaxUrl(), this.ajaxMethod(),
123 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
125 this.infoMsg = this.infoMessage();
126 if (this.stage != "Update")
128 this.nameOrEmail = "";
129 this.user["email"] = "";
130 this.user["name"] = "";
131 // Store our identifiers in local storage (by little anticipation...)
132 localStorage["myid"] = res.id;
133 localStorage["myname"] = res.name;
134 // Also in global object
135 this.$user.id = res.id;
136 this.$user.name = res.name;
140 if (this.stage == "Register")
141 this.stage = "Login";
142 document.getElementById("modalUser").checked = false;