Attempt to fix authenticate + local user data
[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")
5 .card
6 label.modal-close(for="modalUser")
7 h3 {{ stage }}
8 form#userForm(@submit.prevent="onSubmit()")
9 div(v-show="stage!='Login'")
10 fieldset
11 label(for="username") Name
12 input#username(type="text" v-model="user.name")
13 fieldset
14 label(for="useremail") Email
15 input#useremail(type="email" v-model="user.email")
16 fieldset
17 label(for="notifyNew") Notify new moves &amp; games
18 input#notifyNew(type="checkbox" v-model="user.notify")
19 div(v-show="stage=='Login'")
20 fieldset
21 label(for="nameOrEmail") Name or Email
22 input#nameOrEmail(type="text" v-model="nameOrEmail")
23 .button-group
24 button#submit(type="button" @click="onSubmit()")
25 span {{ submitMessage }}
26 i.material-icons send
27 button(v-if="stage!='Update'" @click="toggleStage()")
28 span {{ stage=="Login" ? "Register" : "Login" }}
29 button(v-else onClick="location.replace('/logout')")
30 span Logout
31 #dialog(:style="{display: displayInfo}") {{ infoMsg }}
32 </template>
33
34 <script>
35 import { store } from "@/store";
36 import { checkNameEmail } from "@/data/userCheck";
37 import { ajax } from "@/utils/ajax";
38 export default {
39 name: 'my-upsert-user',
40 data: function() {
41 return {
42 user: Object.assign({}, store.state.user),
43 nameOrEmail: "", //for login
44 stage: (store.state.user.id > 0 ? "Update" : "Login"), //TODO?
45 infoMsg: "",
46 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
47 };
48 },
49 computed: {
50 submitMessage: function() {
51 switch (this.stage)
52 {
53 case "Login":
54 return "Go";
55 case "Register":
56 return "Send";
57 case "Update":
58 return "Apply";
59 }
60 },
61 displayInfo: function() {
62 return (this.infoMsg.length > 0 ? "block" : "none");
63 },
64 },
65 methods: {
66 trySetEnterTime: function(event) {
67 if (!!event.target.checked)
68 this.enterTime = Date.now();
69 },
70 toggleStage: function() {
71 // Loop login <--> register (update is for logged-in users)
72 this.stage = (this.stage == "Login" ? "Register" : "Login");
73 },
74 ajaxUrl: function() {
75 switch (this.stage)
76 {
77 case "Login":
78 return "/sendtoken";
79 case "Register":
80 return "/register";
81 case "Update":
82 return "/update";
83 }
84 },
85 ajaxMethod: function() {
86 switch (this.stage)
87 {
88 case "Login":
89 return "GET";
90 case "Register":
91 return "POST";
92 case "Update":
93 return "PUT";
94 }
95 },
96 infoMessage: function() {
97 switch (this.stage)
98 {
99 case "Login":
100 return "Connection token sent. Check your emails!";
101 case "Register":
102 return "Registration complete! Please check your emails.";
103 case "Update":
104 return "Modifications applied!";
105 }
106 },
107 onSubmit: function() {
108 // Basic anti-bot strategy:
109 const exitTime = Date.now();
110 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
111 return; //silently return, in (curious) case of it was legitimate
112 let error = undefined;
113 if (this.stage == 'Login')
114 {
115 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
116 error = checkNameEmail({[type]: this.nameOrEmail});
117 }
118 else
119 error = checkNameEmail(this.user);
120 if (!!error)
121 return alert(error);
122 this.infoMsg = "Processing... Please wait";
123 ajax(this.ajaxUrl(), this.ajaxMethod(),
124 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
125 res => {
126 this.infoMsg = this.infoMessage();
127 if (this.stage != "Update")
128 this.nameOrEmail = "";
129 setTimeout(() => {
130 this.infoMsg = "";
131 document.getElementById("modalUser").checked = false;
132 }, 2000);
133 },
134 err => {
135 this.infoMsg = "";
136 alert(err);
137 }
138 );
139 },
140 },
141 };
142 </script>