ResetPassword.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. @blur="onInputBlur('email')"
  45. />
  46. </p>
  47. <p class="control">
  48. <a
  49. class="button is-info"
  50. href="#"
  51. @click="submitEmail()"
  52. ><i
  53. class="material-icons icon-with-button"
  54. >mail</i
  55. >Request</a
  56. >
  57. </p>
  58. </div>
  59. <transition name="fadein-helpbox">
  60. <input-help-box
  61. v-if="validation.email.entered"
  62. :valid="validation.email.valid"
  63. :message="validation.email.message"
  64. ></input-help-box>
  65. </transition>
  66. </div>
  67. </div>
  68. <!-- Step 2 -- Enter code -->
  69. <div class="content-box" v-if="step === 2" :key="step">
  70. <h2 class="content-box-title">
  71. Enter the code sent to your email
  72. </h2>
  73. <p
  74. class="content-box-description"
  75. v-if="!this.hasEmailBeenSentAlready"
  76. >
  77. A code has been sent to
  78. <strong>{{ email }}.</strong>
  79. </p>
  80. <p class="content-box-optional-helper">
  81. <a
  82. href="#"
  83. @click="email ? submitEmail() : (step = 1)"
  84. >Request another code</a
  85. >
  86. </p>
  87. <div class="content-box-inputs">
  88. <div class="control is-grouped input-with-button">
  89. <p class="control is-expanded">
  90. <input
  91. class="input"
  92. type="text"
  93. placeholder="Enter code here..."
  94. autofocus
  95. v-model="code"
  96. @keyup.enter="verifyCode()"
  97. />
  98. </p>
  99. <p class="control">
  100. <a
  101. class="button is-info"
  102. href="#"
  103. @click="verifyCode()"
  104. ><i
  105. class="material-icons icon-with-button"
  106. >vpn_key</i
  107. >Verify</a
  108. >
  109. </p>
  110. </div>
  111. </div>
  112. </div>
  113. <!-- Step 3 -- Set new password -->
  114. <div class="content-box" v-if="step === 3" :key="step">
  115. <h2 class="content-box-title">Set a new password</h2>
  116. <p class="content-box-description">
  117. Create a new password for your account.
  118. </p>
  119. <div class="content-box-inputs">
  120. <p class="control is-expanded">
  121. <label for="new-password">New password</label>
  122. <input
  123. class="input"
  124. id="new-password"
  125. type="password"
  126. placeholder="Enter password here..."
  127. v-model="newPassword"
  128. @blur="onInputBlur('newPassword')"
  129. />
  130. </p>
  131. <transition name="fadein-helpbox">
  132. <input-help-box
  133. v-if="validation.newPassword.entered"
  134. :valid="validation.newPassword.valid"
  135. :message="validation.newPassword.message"
  136. ></input-help-box>
  137. </transition>
  138. <p
  139. id="new-password-again-input"
  140. class="control is-expanded"
  141. >
  142. <label for="new-password-again"
  143. >New password again</label
  144. >
  145. <input
  146. class="input"
  147. id="new-password-again"
  148. type="password"
  149. placeholder="Enter password here..."
  150. v-model="newPasswordAgain"
  151. @keyup.enter="changePassword()"
  152. @blur="onInputBlur('newPasswordAgain')"
  153. />
  154. </p>
  155. <transition name="fadein-helpbox">
  156. <input-help-box
  157. v-if="validation.newPasswordAgain.entered"
  158. :valid="validation.newPasswordAgain.valid"
  159. :message="
  160. validation.newPasswordAgain.message
  161. "
  162. ></input-help-box>
  163. </transition>
  164. <a
  165. id="change-password-button"
  166. class="button is-success"
  167. href="#"
  168. @click="changePassword()"
  169. >
  170. Change password</a
  171. >
  172. </div>
  173. </div>
  174. <div
  175. class="content-box reset-status-box"
  176. v-if="step === 4"
  177. :key="step"
  178. >
  179. <i class="material-icons success-icon">check_circle</i>
  180. <h2>Password successfully {{ mode }}</h2>
  181. <router-link
  182. class="button is-dark"
  183. href="#"
  184. to="/settings"
  185. ><i class="material-icons icon-with-button">undo</i
  186. >Return to Settings</router-link
  187. >
  188. </div>
  189. <div
  190. class="content-box reset-status-box"
  191. v-if="step === 5"
  192. :key="step"
  193. >
  194. <i class="material-icons error-icon">error</i>
  195. <h2>
  196. Password {{ mode }} failed, please try again later
  197. </h2>
  198. <router-link
  199. class="button is-dark"
  200. href="#"
  201. to="/settings"
  202. ><i class="material-icons icon-with-button">undo</i
  203. >Return to Settings</router-link
  204. >
  205. </div>
  206. </transition>
  207. </div>
  208. </div>
  209. <main-footer />
  210. </div>
  211. </template>
  212. <script>
  213. import Toast from "toasters";
  214. import { mapGetters } from "vuex";
  215. import MainHeader from "../components/layout/MainHeader.vue";
  216. import MainFooter from "../components/layout/MainFooter.vue";
  217. import InputHelpBox from "../components/ui/InputHelpBox.vue";
  218. import validation from "../validation";
  219. export default {
  220. components: { MainHeader, MainFooter, InputHelpBox },
  221. props: {
  222. mode: {
  223. default: "reset",
  224. enum: ["reset", "set"],
  225. type: String
  226. }
  227. },
  228. data() {
  229. return {
  230. email: "",
  231. hasEmailBeenSentAlready: true,
  232. code: "",
  233. newPassword: "",
  234. newPasswordAgain: "",
  235. validation: {
  236. email: {
  237. entered: false,
  238. valid: false,
  239. message: "Please enter a valid email address."
  240. },
  241. newPassword: {
  242. entered: false,
  243. valid: false,
  244. message: "Please enter a valid password."
  245. },
  246. newPasswordAgain: {
  247. entered: false,
  248. valid: false,
  249. message: "This password must match."
  250. }
  251. },
  252. step: 1
  253. };
  254. },
  255. computed: mapGetters({
  256. socket: "websockets/getSocket"
  257. }),
  258. watch: {
  259. email(value) {
  260. if (
  261. value.indexOf("@") !== value.lastIndexOf("@") ||
  262. !validation.regex.emailSimple.test(value)
  263. ) {
  264. this.validation.email.message =
  265. "Please enter a valid email address.";
  266. this.validation.email.valid = false;
  267. } else {
  268. this.validation.email.message = "Everything looks great!";
  269. this.validation.email.valid = true;
  270. }
  271. },
  272. newPassword(value) {
  273. this.checkPasswordMatch(value, this.newPasswordAgain);
  274. if (!validation.isLength(value, 6, 200)) {
  275. this.validation.newPassword.message =
  276. "Password must have between 6 and 200 characters.";
  277. this.validation.newPassword.valid = false;
  278. } else if (!validation.regex.password.test(value)) {
  279. this.validation.newPassword.message =
  280. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.";
  281. this.validation.newPassword.valid = false;
  282. } else {
  283. this.validation.newPassword.message = "Everything looks great!";
  284. this.validation.newPassword.valid = true;
  285. }
  286. },
  287. newPasswordAgain(value) {
  288. this.checkPasswordMatch(this.newPassword, value);
  289. }
  290. },
  291. methods: {
  292. checkPasswordMatch(newPassword, newPasswordAgain) {
  293. if (newPasswordAgain !== newPassword) {
  294. this.validation.newPasswordAgain.message =
  295. "This password must match.";
  296. this.validation.newPasswordAgain.valid = false;
  297. } else {
  298. this.validation.newPasswordAgain.message =
  299. "Everything looks great!";
  300. this.validation.newPasswordAgain.valid = true;
  301. }
  302. },
  303. onInputBlur(inputName) {
  304. this.validation[inputName].entered = true;
  305. },
  306. submitEmail() {
  307. if (
  308. this.email.indexOf("@") !== this.email.lastIndexOf("@") ||
  309. !validation.regex.emailSimple.test(this.email)
  310. )
  311. return new Toast({
  312. content: "Invalid email format.",
  313. timeout: 8000
  314. });
  315. if (!this.email)
  316. return new Toast({
  317. content: "Email cannot be empty",
  318. timeout: 8000
  319. });
  320. this.hasEmailBeenSentAlready = false;
  321. if (this.mode === "set") {
  322. return this.socket.dispatch("users.requestPassword", res => {
  323. new Toast({ content: res.message, timeout: 8000 });
  324. if (res.status === "success") {
  325. this.step = 2;
  326. }
  327. });
  328. }
  329. return this.socket.dispatch(
  330. "users.requestPasswordReset",
  331. this.email,
  332. res => {
  333. new Toast({ content: res.message, timeout: 8000 });
  334. if (res.status === "success") {
  335. this.code = ""; // in case: already have a code -> request another code
  336. this.step = 2;
  337. } else this.step = 5;
  338. }
  339. );
  340. },
  341. verifyCode() {
  342. if (!this.code)
  343. return new Toast({
  344. content: "Code cannot be empty",
  345. timeout: 8000
  346. });
  347. return this.socket.dispatch(
  348. this.mode === "set"
  349. ? "users.verifyPasswordCode"
  350. : "users.verifyPasswordResetCode",
  351. this.code,
  352. res => {
  353. new Toast({ content: res.message, timeout: 8000 });
  354. if (res.status === "success") {
  355. this.step = 3;
  356. }
  357. }
  358. );
  359. },
  360. changePassword() {
  361. if (
  362. this.validation.newPassword.valid &&
  363. !this.validation.newPasswordAgain.valid
  364. )
  365. return new Toast({
  366. content: "Please ensure the passwords match.",
  367. timeout: 8000
  368. });
  369. if (!this.validation.newPassword.valid)
  370. return new Toast({
  371. content: "Please enter a valid password.",
  372. timeout: 8000
  373. });
  374. return this.socket.dispatch(
  375. this.mode === "set"
  376. ? "users.changePasswordWithCode"
  377. : "users.changePasswordWithResetCode",
  378. this.code,
  379. this.newPassword,
  380. res => {
  381. new Toast({ content: res.message, timeout: 8000 });
  382. if (res.status === "success") this.step = 4;
  383. else this.step = 5;
  384. }
  385. );
  386. }
  387. }
  388. };
  389. </script>
  390. <style lang="scss" scoped>
  391. .night-mode {
  392. .content-box,
  393. .step:not(.selected) {
  394. background-color: var(--dark-grey-3) !important;
  395. }
  396. .label {
  397. color: var(--light-grey-2);
  398. }
  399. .skip-step {
  400. border: 0;
  401. }
  402. }
  403. h1,
  404. h2,
  405. p {
  406. margin: 0;
  407. }
  408. .container {
  409. padding: 25px;
  410. #title {
  411. color: var(--black);
  412. font-size: 42px;
  413. text-align: center;
  414. }
  415. #steps {
  416. display: flex;
  417. align-items: center;
  418. justify-content: center;
  419. height: 50px;
  420. margin-top: 36px;
  421. @media screen and (max-width: 300px) {
  422. display: none;
  423. }
  424. .step {
  425. display: flex;
  426. align-items: center;
  427. justify-content: center;
  428. border-radius: 100%;
  429. border: 1px solid var(--dark-grey);
  430. min-width: 50px;
  431. min-height: 50px;
  432. background-color: var(--white);
  433. font-size: 30px;
  434. cursor: pointer;
  435. &.selected {
  436. background-color: var(--primary-color);
  437. color: var(--white) !important;
  438. border: 0;
  439. }
  440. }
  441. .divider {
  442. display: flex;
  443. justify-content: center;
  444. width: 180px;
  445. height: 1px;
  446. background-color: var(--dark-grey);
  447. }
  448. }
  449. .content-box {
  450. margin-top: 90px;
  451. border-radius: 3px;
  452. background-color: var(--white);
  453. border: 1px solid var(--dark-grey);
  454. max-width: 580px;
  455. padding: 40px;
  456. @media screen and (max-width: 300px) {
  457. margin-top: 30px;
  458. padding: 30px 20px;
  459. }
  460. .content-box-title {
  461. font-size: 25px;
  462. color: var(--black);
  463. }
  464. .content-box-description {
  465. font-size: 14px;
  466. color: var(--dark-grey);
  467. }
  468. .content-box-optional-helper {
  469. margin-top: 15px;
  470. color: var(--primary-color);
  471. text-decoration: underline;
  472. font-size: 16px;
  473. }
  474. .content-box-inputs {
  475. margin-top: 35px;
  476. .input-with-button {
  477. .button {
  478. width: 105px;
  479. }
  480. @media screen and (max-width: 450px) {
  481. flex-direction: column;
  482. }
  483. }
  484. label {
  485. font-size: 11px;
  486. }
  487. #change-password-button {
  488. margin-top: 36px;
  489. width: 175px;
  490. }
  491. }
  492. }
  493. .reset-status-box {
  494. display: flex;
  495. flex-direction: column;
  496. align-items: center;
  497. justify-content: center;
  498. height: 356px;
  499. h2 {
  500. margin-top: 10px;
  501. font-size: 21px;
  502. font-weight: 800;
  503. color: var(--black);
  504. text-align: center;
  505. }
  506. .success-icon {
  507. color: var(--green);
  508. }
  509. .error-icon {
  510. color: var(--red);
  511. }
  512. .success-icon,
  513. .error-icon {
  514. font-size: 125px;
  515. }
  516. .button {
  517. margin-top: 36px;
  518. }
  519. }
  520. }
  521. .steps-fade-enter-active,
  522. .steps-fade-leave-active {
  523. transition: all 0.3s ease;
  524. }
  525. .steps-fade-enter,
  526. .steps-fade-leave-to {
  527. opacity: 0;
  528. }
  529. .skip-step {
  530. background-color: var(--grey-3);
  531. color: var(--white);
  532. }
  533. </style>