Separate client and server codes. Keep everything in one git repo for simplicity
[vchess.git] / client / client_OLD / javascripts / components / upsertUser.js
CommitLineData
b57dbd12 1// Logic to login, or create / update a user (and also logout)
c018b304 2vv = Vue.component('my-upsert-user', {
b57dbd12
BA
3 data: function() {
4 return {
8a477a7e
BA
5 user: user, //initialized with global user object
6 nameOrEmail: "", //for login
7 stage: (!user.email ? "Login" : "Update"),
b57dbd12 8 infoMsg: "",
8a477a7e 9 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
b57dbd12
BA
10 };
11 },
12 template: `
13 <div>
8a477a7e
BA
14 <input id="modalUser" class="modal" type="checkbox"
15 @change="trySetEnterTime"/>
b57dbd12
BA
16 <div role="dialog">
17 <div class="card">
8a477a7e 18 <label class="modal-close" for="modalUser"></label>
b57dbd12 19 <h3>{{ stage }}</h3>
c018b304 20 <form id="userForm" @submit.prevent="onSubmit()">
8a477a7e
BA
21 <div v-show="stage!='Login'">
22 <fieldset>
23 <label for="username">Name</label>
24 <input id="username" type="text" v-model="user.name"/>
25 </fieldset>
26 <fieldset>
27 <label for="useremail">Email</label>
28 <input id="useremail" type="email" v-model="user.email"/>
29 </fieldset>
30 <fieldset>
31 <label for="notifyNew">Notify new moves &amp; games</label>
32 <input id="notifyNew" type="checkbox" v-model="user.notify"/>
33 </fieldset>
34 </div>
35 <div v-show="stage=='Login'">
36 <fieldset>
37 <label for="nameOrEmail">Name or Email</label>
38 <input id="nameOrEmail" type="text" v-model="nameOrEmail"/>
39 </fieldset>
40 </div>
8a477a7e 41 </form>
c018b304
BA
42 <div class="button-group">
43 <button id="submit" @click="onSubmit()">
44 <span>{{ submitMessage }}</span>
45 <i class="material-icons">send</i>
46 </button>
47 <button v-if="stage!='Update'" @click="toggleStage()">
48 <span>{{ stage=="Login" ? "Register" : "Login" }}</span>
49 </button>
50 <button v-if="stage=='Update'" onClick="location.replace('/logout')">
51 <span>Logout</span>
52 </button>
53 </div>
8a477a7e
BA
54 <div id="dialog" :style="{display: displayInfo}">{{ infoMsg }}</div>
55 </div>
b57dbd12
BA
56 </div>
57 </div>
58 `,
59 computed: {
60 submitMessage: function() {
61 switch (this.stage)
62 {
63 case "Login":
64 return "Go";
65 case "Register":
66 return "Send";
67 case "Update":
68 return "Apply";
69 }
70 },
71 displayInfo: function() {
72 return (this.infoMsg.length > 0 ? "block" : "none");
73 },
74 },
75 methods: {
8a477a7e
BA
76 trySetEnterTime: function(event) {
77 if (!!event.target.checked)
78 this.enterTime = Date.now();
79 },
b57dbd12 80 toggleStage: function() {
8a477a7e 81 // Loop login <--> register (update is for logged-in users)
b57dbd12
BA
82 this.stage = (this.stage == "Login" ? "Register" : "Login");
83 },
84 ajaxUrl: function() {
85 switch (this.stage)
86 {
87 case "Login":
88 return "/sendtoken";
89 case "Register":
90 return "/register";
91 case "Update":
92 return "/update";
93 }
94 },
95 ajaxMethod: function() {
96 switch (this.stage)
97 {
98 case "Login":
99 return "GET";
100 case "Register":
101 return "POST";
102 case "Update":
103 return "PUT";
104 }
105 },
106 infoMessage: function() {
107 switch (this.stage)
108 {
109 case "Login":
110 return "Connection token sent. Check your emails!";
111 case "Register":
112 return "Registration complete! Please check your emails.";
113 case "Update":
114 return "Modifications applied!";
115 }
116 },
c018b304 117 onSubmit: function() {
8a477a7e
BA
118 // Basic anti-bot strategy:
119 const exitTime = Date.now();
120 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
121 return; //silently return, in (curious) case of it was legitimate
122 let error = undefined;
123 if (this.stage == 'Login')
124 {
125 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
126 error = checkNameEmail({[type]: this.nameOrEmail});
127 }
128 else
129 error = checkNameEmail(this.user);
130 if (!!error)
131 return alert(error);
b57dbd12
BA
132 this.infoMsg = "Processing... Please wait";
133 ajax(this.ajaxUrl(), this.ajaxMethod(),
8a477a7e 134 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
b57dbd12
BA
135 res => {
136 this.infoMsg = this.infoMessage();
137 if (this.stage != "Update")
138 {
8a477a7e 139 this.nameOrEmail = "";
b57dbd12
BA
140 this.user["email"] = "";
141 this.user["name"] = "";
625022fd
BA
142 // Store our identifiers in local storage (by little anticipation...)
143 localStorage["myid"] = res.id;
144 localStorage["myname"] = res.name;
b57dbd12
BA
145 }
146 setTimeout(() => {
147 this.infoMsg = "";
c018b304
BA
148 if (this.stage == "Register")
149 this.stage = "Login";
b57dbd12
BA
150 document.getElementById("modalUser").checked = false;
151 }, 2000);
8a477a7e
BA
152 },
153 err => {
154 this.infoMsg = "";
155 alert(err);
b57dbd12
BA
156 }
157 );
158 },
159 }
160});