| 1 | <template lang="pug"> |
| 2 | div |
| 3 | input#modalUser.modal(type="checkbox" @change="trySetEnterTime($event)") |
| 4 | div(role="dialog" data-checkbox="modalUser") |
| 5 | .card |
| 6 | label.modal-close(for="modalUser") |
| 7 | h3.section {{ st.tr[stage] }} |
| 8 | form(@submit.prevent="onSubmit()" @keyup.enter="onSubmit()") |
| 9 | div(v-show="stage!='Login'") |
| 10 | fieldset |
| 11 | label(for="username") {{ st.tr["User 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(@click="onSubmit()") |
| 25 | span {{ st.tr[submitMessage] }} |
| 26 | button(v-if="stage!='Update'" type="button" @click="toggleStage()") |
| 27 | span {{ st.tr[stage=="Login" ? "Register" : "Login"] }} |
| 28 | button(v-else type="button" @click="doLogout()") |
| 29 | span {{ st.tr["Logout"] }} |
| 30 | #dialog.text-center {{ 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 | this.st.user.email = newValue; |
| 52 | this.st.user.name = ""; |
| 53 | } else { |
| 54 | this.st.user.name = newValue; |
| 55 | this.st.user.email = ""; |
| 56 | } |
| 57 | } |
| 58 | }, |
| 59 | computed: { |
| 60 | submitMessage: function() { |
| 61 | switch (this.stage) { |
| 62 | case "Login": |
| 63 | return "Go"; |
| 64 | case "Register": |
| 65 | return "Send"; |
| 66 | case "Update": |
| 67 | return "Apply"; |
| 68 | } |
| 69 | return "Never reached"; |
| 70 | }, |
| 71 | stage: function() { |
| 72 | return this.st.user.id > 0 ? "Update" : this.logStage; |
| 73 | } |
| 74 | }, |
| 75 | methods: { |
| 76 | trySetEnterTime: function(event) { |
| 77 | if (event.target.checked) { |
| 78 | this.infoMsg = ""; |
| 79 | this.enterTime = Date.now(); |
| 80 | } |
| 81 | }, |
| 82 | toggleStage: function() { |
| 83 | // Loop login <--> register (update is for logged-in users) |
| 84 | this.logStage = this.logStage == "Login" ? "Register" : "Login"; |
| 85 | }, |
| 86 | ajaxUrl: function() { |
| 87 | switch (this.stage) { |
| 88 | case "Login": |
| 89 | return "/sendtoken"; |
| 90 | case "Register": |
| 91 | return "/register"; |
| 92 | case "Update": |
| 93 | return "/update"; |
| 94 | } |
| 95 | return "Never reached"; |
| 96 | }, |
| 97 | ajaxMethod: function() { |
| 98 | switch (this.stage) { |
| 99 | case "Login": |
| 100 | return "GET"; |
| 101 | case "Register": |
| 102 | return "POST"; |
| 103 | case "Update": |
| 104 | return "PUT"; |
| 105 | } |
| 106 | return "Never reached"; |
| 107 | }, |
| 108 | infoMessage: function() { |
| 109 | switch (this.stage) { |
| 110 | case "Login": |
| 111 | return "Connection token sent. Check your emails!"; |
| 112 | case "Register": |
| 113 | return "Registration complete! Please check your emails"; |
| 114 | case "Update": |
| 115 | return "Modifications applied!"; |
| 116 | } |
| 117 | return "Never reached"; |
| 118 | }, |
| 119 | onSubmit: function() { |
| 120 | // Basic anti-bot strategy: |
| 121 | const exitTime = Date.now(); |
| 122 | if (this.stage == "Register" && exitTime - this.enterTime < 5000) return; |
| 123 | let error = undefined; |
| 124 | if (this.stage == "Login") { |
| 125 | const type = this.nameOrEmail.indexOf("@") >= 0 ? "email" : "name"; |
| 126 | error = checkNameEmail({ [type]: this.nameOrEmail }); |
| 127 | } else error = checkNameEmail(this.st.user); |
| 128 | if (error) { |
| 129 | alert(error); |
| 130 | return; |
| 131 | } |
| 132 | this.infoMsg = "Processing... Please wait"; |
| 133 | ajax( |
| 134 | this.ajaxUrl(), |
| 135 | this.ajaxMethod(), |
| 136 | this.stage == "Login" |
| 137 | ? { nameOrEmail: this.nameOrEmail } |
| 138 | : this.st.user, |
| 139 | () => { |
| 140 | this.infoMsg = this.infoMessage(); |
| 141 | if (this.stage != "Update") this.nameOrEmail = ""; |
| 142 | }, |
| 143 | err => { |
| 144 | this.infoMsg = ""; |
| 145 | alert(err); |
| 146 | } |
| 147 | ); |
| 148 | }, |
| 149 | doLogout: function() { |
| 150 | document.getElementById("modalUser").checked = false; |
| 151 | this.$router.push("/logout"); |
| 152 | } |
| 153 | } |
| 154 | }; |
| 155 | </script> |
| 156 | |
| 157 | <style lang="sass" scoped> |
| 158 | [type="checkbox"].modal+div .card |
| 159 | max-width: 370px |
| 160 | max-height: 100% |
| 161 | #dialog |
| 162 | padding: 5px |
| 163 | color: blue |
| 164 | </style> |