ResetPassword.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <div>
  3. <metadata
  4. :title="mode === 'reset' ? 'Reset password' : 'Set password'"
  5. />
  6. <main-header />
  7. <div class="container">
  8. <div class="content-wrapper">
  9. <h1 id="title">
  10. {{ mode === "reset" ? "Reset" : "Set" }} your password
  11. </h1>
  12. <div id="steps">
  13. <p class="step" :class="{ selected: step === 1 }">1</p>
  14. <span class="divider"></span>
  15. <p class="step" :class="{ selected: step === 2 }">2</p>
  16. <span class="divider"></span>
  17. <p class="step" :class="{ selected: step === 3 }">3</p>
  18. </div>
  19. <transition name="steps-fade" mode="out-in">
  20. <!-- Step 1 -- Enter email address -->
  21. <div class="content-box" v-if="step === 1" :key="step">
  22. <h2 class="content-box-title">
  23. Enter your email address
  24. </h2>
  25. <p class="content-box-description">
  26. We will send a code to your email address to verify
  27. your identity.
  28. </p>
  29. <p class="content-box-optional-helper">
  30. <a href="#" @click="step = 2"
  31. >Already have a code?</a
  32. >
  33. </p>
  34. <div class="content-box-inputs">
  35. <div class="control is-grouped input-with-button">
  36. <p class="control is-expanded">
  37. <input
  38. class="input"
  39. type="email"
  40. placeholder="Enter email address here..."
  41. autofocus
  42. v-model="email"
  43. @keyup.enter="submitEmail()"
  44. @keypress="onInput('email')"
  45. @paste="onInput('email')"
  46. />
  47. </p>
  48. <p class="control">
  49. <a
  50. class="button is-info"
  51. href="#"
  52. @click="submitEmail()"
  53. ><i
  54. class="material-icons icon-with-button"
  55. >mail</i
  56. >Request</a
  57. >
  58. </p>
  59. </div>
  60. <transition name="fadein-helpbox">
  61. <input-help-box
  62. :entered="validation.email.entered"
  63. :valid="validation.email.valid"
  64. :message="validation.email.message"
  65. />
  66. </transition>
  67. </div>
  68. </div>
  69. <!-- Step 2 -- Enter code -->
  70. <div class="content-box" v-if="step === 2" :key="step">
  71. <h2 class="content-box-title">
  72. Enter the code sent to your email
  73. </h2>
  74. <p
  75. class="content-box-description"
  76. v-if="!this.hasEmailBeenSentAlready"
  77. >
  78. A code has been sent to
  79. <strong>{{ email }}.</strong>
  80. </p>
  81. <p class="content-box-optional-helper">
  82. <a
  83. href="#"
  84. @click="email ? submitEmail() : (step = 1)"
  85. >Request another code</a
  86. >
  87. </p>
  88. <div class="content-box-inputs">
  89. <div class="control is-grouped input-with-button">
  90. <p class="control is-expanded">
  91. <input
  92. class="input"
  93. type="text"
  94. placeholder="Enter code here..."
  95. autofocus
  96. v-model="code"
  97. @keyup.enter="verifyCode()"
  98. />
  99. </p>
  100. <p class="control">
  101. <a
  102. class="button is-info"
  103. href="#"
  104. @click="verifyCode()"
  105. ><i
  106. class="material-icons icon-with-button"
  107. >vpn_key</i
  108. >Verify</a
  109. >
  110. </p>
  111. </div>
  112. </div>
  113. </div>
  114. <!-- Step 3 -- Set new password -->
  115. <div class="content-box" v-if="step === 3" :key="step">
  116. <h2 class="content-box-title">Set a new password</h2>
  117. <p class="content-box-description">
  118. Create a new password for your account.
  119. </p>
  120. <div class="content-box-inputs">
  121. <p class="control is-expanded">
  122. <label for="new-password">New password</label>
  123. <input
  124. class="input"
  125. id="new-password"
  126. type="password"
  127. placeholder="Enter password here..."
  128. v-model="newPassword"
  129. @keypress="onInput('newPassword')"
  130. @paste="onInput('newPassword')"
  131. />
  132. </p>
  133. <transition name="fadein-helpbox">
  134. <input-help-box
  135. :entered="validation.newPassword.entered"
  136. :valid="validation.newPassword.valid"
  137. :message="validation.newPassword.message"
  138. />
  139. </transition>
  140. <p
  141. id="new-password-again-input"
  142. class="control is-expanded"
  143. >
  144. <label for="new-password-again"
  145. >New password again</label
  146. >
  147. <input
  148. class="input"
  149. id="new-password-again"
  150. type="password"
  151. placeholder="Enter password here..."
  152. v-model="newPasswordAgain"
  153. @keyup.enter="changePassword()"
  154. @keypress="onInput('newPasswordAgain')"
  155. @paste="onInput('newPasswordAgain')"
  156. />
  157. </p>
  158. <transition name="fadein-helpbox">
  159. <input-help-box
  160. :entered="
  161. validation.newPasswordAgain.entered
  162. "
  163. :valid="validation.newPasswordAgain.valid"
  164. :message="
  165. validation.newPasswordAgain.message
  166. "
  167. />
  168. </transition>
  169. <a
  170. id="change-password-button"
  171. class="button is-success"
  172. href="#"
  173. @click="changePassword()"
  174. >
  175. Change password</a
  176. >
  177. </div>
  178. </div>
  179. <div
  180. class="content-box reset-status-box"
  181. v-if="step === 4"
  182. :key="step"
  183. >
  184. <i class="material-icons success-icon">check_circle</i>
  185. <h2>Password successfully {{ mode }}</h2>
  186. <router-link
  187. class="button is-dark"
  188. href="#"
  189. to="/settings"
  190. ><i class="material-icons icon-with-button">undo</i
  191. >Return to Settings</router-link
  192. >
  193. </div>
  194. <div
  195. class="content-box reset-status-box"
  196. v-if="step === 5"
  197. :key="step"
  198. >
  199. <i class="material-icons error-icon">error</i>
  200. <h2>
  201. Password {{ mode }} failed, please try again later
  202. </h2>
  203. <router-link
  204. class="button is-dark"
  205. href="#"
  206. to="/settings"
  207. ><i class="material-icons icon-with-button">undo</i
  208. >Return to Settings</router-link
  209. >
  210. </div>
  211. </transition>
  212. </div>
  213. </div>
  214. <main-footer />
  215. </div>
  216. </template>
  217. <script>
  218. import Toast from "toasters";
  219. import { mapGetters } from "vuex";
  220. import MainHeader from "@/components/layout/MainHeader.vue";
  221. import MainFooter from "@/components/layout/MainFooter.vue";
  222. import InputHelpBox from "@/components/InputHelpBox.vue";
  223. import validation from "@/validation";
  224. export default {
  225. components: { MainHeader, MainFooter, InputHelpBox },
  226. props: {
  227. mode: {
  228. default: "reset",
  229. enum: ["reset", "set"],
  230. type: String
  231. }
  232. },
  233. data() {
  234. return {
  235. email: "",
  236. hasEmailBeenSentAlready: true,
  237. code: "",
  238. newPassword: "",
  239. newPasswordAgain: "",
  240. validation: {
  241. email: {
  242. entered: false,
  243. valid: false,
  244. message: "Please enter a valid email address."
  245. },
  246. newPassword: {
  247. entered: false,
  248. valid: false,
  249. message:
  250. "Include at least one lowercase letter, one uppercase letter, one number and one special character."
  251. },
  252. newPasswordAgain: {
  253. entered: false,
  254. valid: false,
  255. message: "This password must match."
  256. }
  257. },
  258. step: 1
  259. };
  260. },
  261. computed: mapGetters({
  262. socket: "websockets/getSocket"
  263. }),
  264. watch: {
  265. email(value) {
  266. if (
  267. value.indexOf("@") !== value.lastIndexOf("@") ||
  268. !validation.regex.emailSimple.test(value)
  269. ) {
  270. this.validation.email.message =
  271. "Please enter a valid email address.";
  272. this.validation.email.valid = false;
  273. } else {
  274. this.validation.email.message = "Everything looks great!";
  275. this.validation.email.valid = true;
  276. }
  277. },
  278. newPassword(value) {
  279. this.checkPasswordMatch(value, this.newPasswordAgain);
  280. if (!validation.isLength(value, 6, 200)) {
  281. this.validation.newPassword.message =
  282. "Password must have between 6 and 200 characters.";
  283. this.validation.newPassword.valid = false;
  284. } else if (!validation.regex.password.test(value)) {
  285. this.validation.newPassword.message =
  286. "Include at least one lowercase letter, one uppercase letter, one number and one special character.";
  287. this.validation.newPassword.valid = false;
  288. } else {
  289. this.validation.newPassword.message = "Everything looks great!";
  290. this.validation.newPassword.valid = true;
  291. }
  292. },
  293. newPasswordAgain(value) {
  294. this.checkPasswordMatch(this.newPassword, value);
  295. }
  296. },
  297. methods: {
  298. checkPasswordMatch(newPassword, newPasswordAgain) {
  299. if (newPasswordAgain !== newPassword) {
  300. this.validation.newPasswordAgain.message =
  301. "This password must match.";
  302. this.validation.newPasswordAgain.valid = false;
  303. } else {
  304. this.validation.newPasswordAgain.message =
  305. "Everything looks great!";
  306. this.validation.newPasswordAgain.valid = true;
  307. }
  308. },
  309. onInput(inputName) {
  310. this.validation[inputName].entered = true;
  311. },
  312. submitEmail() {
  313. if (
  314. this.email.indexOf("@") !== this.email.lastIndexOf("@") ||
  315. !validation.regex.emailSimple.test(this.email)
  316. )
  317. return new Toast("Invalid email format.");
  318. if (!this.email) return new Toast("Email cannot be empty");
  319. this.hasEmailBeenSentAlready = false;
  320. if (this.mode === "set") {
  321. return this.socket.dispatch("users.requestPassword", res => {
  322. new Toast(res.message);
  323. if (res.status === "success") this.step = 2;
  324. });
  325. }
  326. return this.socket.dispatch(
  327. "users.requestPasswordReset",
  328. this.email,
  329. res => {
  330. new Toast(res.message);
  331. if (res.status === "success") {
  332. this.code = ""; // in case: already have a code -> request another code
  333. this.step = 2;
  334. } else this.step = 5;
  335. }
  336. );
  337. },
  338. verifyCode() {
  339. if (!this.code) return new Toast("Code cannot be empty");
  340. return this.socket.dispatch(
  341. this.mode === "set"
  342. ? "users.verifyPasswordCode"
  343. : "users.verifyPasswordResetCode",
  344. this.code,
  345. res => {
  346. new Toast(res.message);
  347. if (res.status === "success") this.step = 3;
  348. }
  349. );
  350. },
  351. changePassword() {
  352. if (
  353. this.validation.newPassword.valid &&
  354. !this.validation.newPasswordAgain.valid
  355. )
  356. return new Toast("Please ensure the passwords match.");
  357. if (!this.validation.newPassword.valid)
  358. return new Toast("Please enter a valid password.");
  359. return this.socket.dispatch(
  360. this.mode === "set"
  361. ? "users.changePasswordWithCode"
  362. : "users.changePasswordWithResetCode",
  363. this.code,
  364. this.newPassword,
  365. res => {
  366. new Toast(res.message);
  367. if (res.status === "success") this.step = 4;
  368. else this.step = 5;
  369. }
  370. );
  371. }
  372. }
  373. };
  374. </script>
  375. <style lang="scss" scoped>
  376. .night-mode {
  377. .label {
  378. color: var(--light-grey-2);
  379. }
  380. .skip-step {
  381. border: 0;
  382. }
  383. }
  384. h1,
  385. h2,
  386. p {
  387. margin: 0;
  388. }
  389. .container {
  390. padding: 25px;
  391. #title {
  392. color: var(--black);
  393. font-size: 42px;
  394. text-align: center;
  395. }
  396. .reset-status-box {
  397. display: flex;
  398. flex-direction: column;
  399. align-items: center;
  400. justify-content: center;
  401. height: 356px;
  402. h2 {
  403. margin-top: 10px;
  404. font-size: 21px;
  405. font-weight: 800;
  406. color: var(--black);
  407. text-align: center;
  408. }
  409. .success-icon {
  410. color: var(--green);
  411. }
  412. .error-icon {
  413. color: var(--red);
  414. }
  415. .success-icon,
  416. .error-icon {
  417. font-size: 125px;
  418. }
  419. .button {
  420. margin-top: 36px;
  421. }
  422. }
  423. }
  424. .control {
  425. margin-bottom: 2px !important;
  426. }
  427. </style>