Fix login/register system
[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
1aeed627
BA
15 label(for="useremail") Email
16 input#useremail(type="email" v-model="user.email")
c66a829b 17 fieldset
1aeed627
BA
18 label(for="notifyNew") Notify new moves &amp; games
19 input#notifyNew(type="checkbox" v-model="user.notify")
c66a829b
BA
20 div(v-show="stage=='Login'")
21 fieldset
1aeed627
BA
22 label(for="nameOrEmail") Name or Email
23 input#nameOrEmail(type="text" v-model="nameOrEmail")
c66a829b 24 .button-group
1aeed627 25 button#submit(type="button" @click="onSubmit()")
c66a829b
BA
26 span {{ submitMessage }}
27 i.material-icons send
28 button(v-if="stage!='Update'" @click="toggleStage()")
29 span {{ stage=="Login" ? "Register" : "Login" }}
1aeed627 30 button(v-else onClick="location.replace('/logout')")
c66a829b
BA
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 {
1aeed627 43 user: store.state.user,
f05815d7 44 nameOrEmail: "", //for login
1aeed627 45 stage: (store.state.user.id > 0 ? "Update" : "Login"), //TODO?
f05815d7
BA
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 => {
f05815d7
BA
127 this.infoMsg = this.infoMessage();
128 if (this.stage != "Update")
129 {
130 this.nameOrEmail = "";
131 this.user["email"] = "";
1aeed627
BA
132 // Update global object
133 this.user["name"] = res.name;
134 this.user["id"] = res.id;
f05815d7
BA
135 // Store our identifiers in local storage (by little anticipation...)
136 localStorage["myid"] = res.id;
137 localStorage["myname"] = res.name;
f05815d7
BA
138 }
139 setTimeout(() => {
140 this.infoMsg = "";
141 if (this.stage == "Register")
142 this.stage = "Login";
143 document.getElementById("modalUser").checked = false;
144 }, 2000);
145 },
146 err => {
147 this.infoMsg = "";
148 alert(err);
149 }
150 );
151 },
152 },
c66a829b
BA
153};
154</script>