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";
37 import { checkNameEmail } from "@/data/userCheck";
38 import { ajax } from "@/utils/ajax";
40 name: 'my-upsert-user',
43 user: store.state.user, //initialized with global user object
44 nameOrEmail: "", //for login
45 stage: (!store.state.user.id ? "Login" : "Update"),
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,
128 console.log("receive login infos");
131 this.infoMsg = this.infoMessage();
132 if (this.stage != "Update")
134 this.nameOrEmail = "";
135 this.user["email"] = "";
136 this.user["name"] = "";
138 debugger; //TODO: 2 passages ici au lieu d'1 lors du register
140 // Store our identifiers in local storage (by little anticipation...)
141 localStorage["myid"] = res.id;
142 localStorage["myname"] = res.name;
143 // Also in global object
144 this.st.user.id = res.id;
145 this.st.user.name = res.name;
149 if (this.stage == "Register")
150 this.stage = "Login";
151 document.getElementById("modalUser").checked = false;