| 1 | // Logic to login, or create / update a user (and also logout) |
| 2 | <template lang="pug"> |
| 3 | div |
| 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 |
| 16 | input#useremail(type="email" v-model="user.email") |
| 17 | fieldset |
| 18 | label(for="notifyNew") Notify new moves & games |
| 19 | input#notifyNew(type="checkbox" v-model="user.notify") |
| 20 | div(v-show="stage=='Login'") |
| 21 | fieldset |
| 22 | label(for="nameOrEmail") Name or Email |
| 23 | input#nameOrEmail(type="text" v-model="nameOrEmail") |
| 24 | .button-group |
| 25 | button#submit(type="button" @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-else onClick="location.replace('/logout')") |
| 31 | span Logout |
| 32 | #dialog(:style="{display: displayInfo}") {{ infoMsg }} |
| 33 | </template> |
| 34 | |
| 35 | <script> |
| 36 | import { store } from "@/store"; |
| 37 | import { checkNameEmail } from "@/data/userCheck"; |
| 38 | import { ajax } from "@/utils/ajax"; |
| 39 | export default { |
| 40 | name: 'my-upsert-user', |
| 41 | data: function() { |
| 42 | return { |
| 43 | user: store.state.user, |
| 44 | nameOrEmail: "", //for login |
| 45 | stage: (store.state.user.id > 0 ? "Update" : "Login"), //TODO? |
| 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 | this.infoMsg = this.infoMessage(); |
| 128 | if (this.stage != "Update") |
| 129 | { |
| 130 | this.nameOrEmail = ""; |
| 131 | this.user["email"] = ""; |
| 132 | // Update global object |
| 133 | this.user["name"] = res.name; |
| 134 | this.user["id"] = res.id; |
| 135 | // Store our identifiers in local storage (by little anticipation...) |
| 136 | localStorage["myid"] = res.id; |
| 137 | localStorage["myname"] = res.name; |
| 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 | }, |
| 153 | }; |
| 154 | </script> |