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