Experimental improved behavior of login/logout/multitabs
[vchess.git] / client / src / components / UpsertUser.vue
1 <template lang="pug">
2 div
3 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
4 div(role="dialog" data-checkbox="modalUser")
5 .card
6 label.modal-close(for="modalUser")
7 h3 {{ st.tr[stage] }}
8 form#userForm(@submit.prevent="onSubmit()" @keyup.enter="onSubmit")
9 div(v-show="stage!='Login'")
10 fieldset
11 label(for="username") {{ st.tr["Name"] }}
12 input#username(type="text" v-model="st.user.name")
13 fieldset
14 label(for="useremail") {{ st.tr["Email"] }}
15 input#useremail(type="email" v-model="st.user.email")
16 fieldset
17 label(for="notifyNew") {{ st.tr["Notifications by email"] }}
18 input#notifyNew(type="checkbox" v-model="st.user.notify")
19 div(v-show="stage=='Login'")
20 fieldset
21 label(for="nameOrEmail") {{ st.tr["Name or Email"] }}
22 input#nameOrEmail(type="text" v-model="nameOrEmail")
23 .button-group
24 button#submit(type="button" @click="onSubmit()")
25 span {{ st.tr[submitMessage] }}
26 button(v-if="stage!='Update'" @click="toggleStage()")
27 span {{ st.tr[stage=="Login" ? "Register" : "Login"] }}
28 button#logoutBtn(v-else @click="doLogout()")
29 span {{ st.tr["Logout"] }}
30 #dialog(:style="{display: displayInfo}") {{ st.tr[infoMsg] }}
31 </template>
32
33 <script>
34 import { store } from "@/store";
35 import { checkNameEmail } from "@/data/userCheck";
36 import { ajax } from "@/utils/ajax";
37 export default {
38 name: 'my-upsert-user',
39 data: function() {
40 return {
41 nameOrEmail: "", //for login
42 logStage: "Login", //or Register
43 infoMsg: "",
44 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
45 st: store.state,
46 };
47 },
48 watch: {
49 nameOrEmail: function(newValue) {
50 if (newValue.indexOf('@') >= 0)
51 {
52 this.st.user.email = newValue;
53 this.st.user.name = "";
54 }
55 else
56 {
57 this.st.user.name = newValue;
58 this.st.user.email = "";
59 }
60 },
61 },
62 computed: {
63 submitMessage: function() {
64 switch (this.stage)
65 {
66 case "Login":
67 return "Go";
68 case "Register":
69 return "Send";
70 case "Update":
71 return "Apply";
72 }
73 },
74 displayInfo: function() {
75 return (this.infoMsg.length > 0 ? "block" : "none");
76 },
77 stage: function() {
78 return this.st.user.id > 0 ? "Update" : this.logStage;
79 },
80 },
81 methods: {
82 trySetEnterTime: function(event) {
83 if (!!event.target.checked)
84 this.enterTime = Date.now();
85 },
86 toggleStage: function() {
87 // Loop login <--> register (update is for logged-in users)
88 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
89 },
90 ajaxUrl: function() {
91 switch (this.stage)
92 {
93 case "Login":
94 return "/sendtoken";
95 case "Register":
96 return "/register";
97 case "Update":
98 return "/update";
99 }
100 },
101 ajaxMethod: function() {
102 switch (this.stage)
103 {
104 case "Login":
105 return "GET";
106 case "Register":
107 return "POST";
108 case "Update":
109 return "PUT";
110 }
111 },
112 infoMessage: function() {
113 switch (this.stage)
114 {
115 case "Login":
116 return "Connection token sent. Check your emails!";
117 case "Register":
118 return "Registration complete! Please check your emails";
119 case "Update":
120 return "Modifications applied!";
121 }
122 },
123 onSubmit: function() {
124 // Basic anti-bot strategy:
125 const exitTime = Date.now();
126 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
127 return; //silently return, in (curious) case of it was legitimate
128 let error = undefined;
129 if (this.stage == 'Login')
130 {
131 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
132 error = checkNameEmail({[type]: this.nameOrEmail});
133 }
134 else
135 error = checkNameEmail(this.st.user);
136 if (!!error)
137 return alert(error);
138 this.infoMsg = "Processing... Please wait";
139 ajax(this.ajaxUrl(), this.ajaxMethod(),
140 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.st.user,
141 res => {
142 this.infoMsg = this.infoMessage();
143 if (this.stage != "Update")
144 this.nameOrEmail = "";
145 setTimeout(() => {
146 this.infoMsg = "";
147 document.getElementById("modalUser").checked = false;
148 }, 2000);
149 },
150 err => {
151 this.infoMsg = "";
152 alert(err);
153 }
154 );
155 },
156 doLogout: function() {
157 document.getElementById("modalUser").checked = false;
158 this.$router.push("/logout");
159 },
160 },
161 };
162 </script>