ResetPassword.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 MainHeader from "../components/layout/MainHeader.vue";
  215. import MainFooter from "../components/layout/MainFooter.vue";
  216. import InputHelpBox from "../components/ui/InputHelpBox.vue";
  217. import io from "../io";
  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. watch: {
  256. email(value) {
  257. if (
  258. value.indexOf("@") !== value.lastIndexOf("@") ||
  259. !validation.regex.emailSimple.test(value)
  260. ) {
  261. this.validation.email.message =
  262. "Please enter a valid email address.";
  263. this.validation.email.valid = false;
  264. } else {
  265. this.validation.email.message = "Everything looks great!";
  266. this.validation.email.valid = true;
  267. }
  268. },
  269. newPassword(value) {
  270. this.checkPasswordMatch(value, this.newPasswordAgain);
  271. if (!validation.isLength(value, 6, 200)) {
  272. this.validation.newPassword.message =
  273. "Password must have between 6 and 200 characters.";
  274. this.validation.newPassword.valid = false;
  275. } else if (!validation.regex.password.test(value)) {
  276. this.validation.newPassword.message =
  277. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.";
  278. this.validation.newPassword.valid = false;
  279. } else {
  280. this.validation.newPassword.message = "Everything looks great!";
  281. this.validation.newPassword.valid = true;
  282. }
  283. },
  284. newPasswordAgain(value) {
  285. this.checkPasswordMatch(this.newPassword, value);
  286. }
  287. },
  288. mounted() {
  289. io.getSocket(socket => {
  290. this.socket = socket;
  291. });
  292. },
  293. methods: {
  294. checkPasswordMatch(newPassword, newPasswordAgain) {
  295. if (newPasswordAgain !== newPassword) {
  296. this.validation.newPasswordAgain.message =
  297. "This password must match.";
  298. this.validation.newPasswordAgain.valid = false;
  299. } else {
  300. this.validation.newPasswordAgain.message =
  301. "Everything looks great!";
  302. this.validation.newPasswordAgain.valid = true;
  303. }
  304. },
  305. onInputBlur(inputName) {
  306. this.validation[inputName].entered = true;
  307. },
  308. submitEmail() {
  309. if (
  310. this.email.indexOf("@") !== this.email.lastIndexOf("@") ||
  311. !validation.regex.emailSimple.test(this.email)
  312. )
  313. return new Toast({
  314. content: "Invalid email format.",
  315. timeout: 8000
  316. });
  317. if (!this.email)
  318. return new Toast({
  319. content: "Email cannot be empty",
  320. timeout: 8000
  321. });
  322. this.hasEmailBeenSentAlready = false;
  323. if (this.mode === "set") {
  324. return this.socket.dispatch("users.requestPassword", res => {
  325. new Toast({ content: res.message, timeout: 8000 });
  326. if (res.status === "success") {
  327. this.step = 2;
  328. }
  329. });
  330. }
  331. return this.socket.dispatch(
  332. "users.requestPasswordReset",
  333. this.email,
  334. res => {
  335. new Toast({ content: res.message, timeout: 8000 });
  336. if (res.status === "success") {
  337. this.code = ""; // in case: already have a code -> request another code
  338. this.step = 2;
  339. } else this.step = 5;
  340. }
  341. );
  342. },
  343. verifyCode() {
  344. if (!this.code)
  345. return new Toast({
  346. content: "Code cannot be empty",
  347. timeout: 8000
  348. });
  349. return this.socket.dispatch(
  350. this.mode === "set"
  351. ? "users.verifyPasswordCode"
  352. : "users.verifyPasswordResetCode",
  353. this.code,
  354. res => {
  355. new Toast({ content: res.message, timeout: 8000 });
  356. if (res.status === "success") {
  357. this.step = 3;
  358. }
  359. }
  360. );
  361. },
  362. changePassword() {
  363. if (
  364. this.validation.newPassword.valid &&
  365. !this.validation.newPasswordAgain.valid
  366. )
  367. return new Toast({
  368. content: "Please ensure the passwords match.",
  369. timeout: 8000
  370. });
  371. if (!this.validation.newPassword.valid)
  372. return new Toast({
  373. content: "Please enter a valid password.",
  374. timeout: 8000
  375. });
  376. return this.socket.dispatch(
  377. this.mode === "set"
  378. ? "users.changePasswordWithCode"
  379. : "users.changePasswordWithResetCode",
  380. this.code,
  381. this.newPassword,
  382. res => {
  383. new Toast({ content: res.message, timeout: 8000 });
  384. if (res.status === "success") this.step = 4;
  385. else this.step = 5;
  386. }
  387. );
  388. }
  389. }
  390. };
  391. </script>
  392. <style lang="scss" scoped>
  393. .night-mode {
  394. .content-box,
  395. .step:not(.selected) {
  396. background-color: var(--dark-grey-3) !important;
  397. }
  398. .label {
  399. color: var(--light-grey-2);
  400. }
  401. .skip-step {
  402. border: 0;
  403. }
  404. }
  405. h1,
  406. h2,
  407. p {
  408. margin: 0;
  409. }
  410. .container {
  411. padding: 25px;
  412. #title {
  413. color: var(--black);
  414. font-size: 42px;
  415. text-align: center;
  416. }
  417. #steps {
  418. display: flex;
  419. align-items: center;
  420. justify-content: center;
  421. height: 50px;
  422. margin-top: 36px;
  423. @media screen and (max-width: 300px) {
  424. display: none;
  425. }
  426. .step {
  427. display: flex;
  428. align-items: center;
  429. justify-content: center;
  430. border-radius: 100%;
  431. border: 1px solid var(--dark-grey);
  432. min-width: 50px;
  433. min-height: 50px;
  434. background-color: var(--white);
  435. font-size: 30px;
  436. cursor: pointer;
  437. &.selected {
  438. background-color: var(--primary-color);
  439. color: var(--white) !important;
  440. border: 0;
  441. }
  442. }
  443. .divider {
  444. display: flex;
  445. justify-content: center;
  446. width: 180px;
  447. height: 1px;
  448. background-color: var(--dark-grey);
  449. }
  450. }
  451. .content-box {
  452. margin-top: 90px;
  453. border-radius: 3px;
  454. background-color: var(--white);
  455. border: 1px solid var(--dark-grey);
  456. max-width: 580px;
  457. padding: 40px;
  458. @media screen and (max-width: 300px) {
  459. margin-top: 30px;
  460. padding: 30px 20px;
  461. }
  462. .content-box-title {
  463. font-size: 25px;
  464. color: var(--black);
  465. }
  466. .content-box-description {
  467. font-size: 14px;
  468. color: var(--dark-grey);
  469. }
  470. .content-box-optional-helper {
  471. margin-top: 15px;
  472. color: var(--primary-color);
  473. text-decoration: underline;
  474. font-size: 16px;
  475. }
  476. .content-box-inputs {
  477. margin-top: 35px;
  478. .input-with-button {
  479. .button {
  480. width: 105px;
  481. }
  482. @media screen and (max-width: 450px) {
  483. flex-direction: column;
  484. }
  485. }
  486. label {
  487. font-size: 11px;
  488. }
  489. #change-password-button {
  490. margin-top: 36px;
  491. width: 175px;
  492. }
  493. }
  494. }
  495. .reset-status-box {
  496. display: flex;
  497. flex-direction: column;
  498. align-items: center;
  499. justify-content: center;
  500. height: 356px;
  501. h2 {
  502. margin-top: 10px;
  503. font-size: 21px;
  504. font-weight: 800;
  505. color: var(--black);
  506. text-align: center;
  507. }
  508. .success-icon {
  509. color: var(--green);
  510. }
  511. .error-icon {
  512. color: var(--red);
  513. }
  514. .success-icon,
  515. .error-icon {
  516. font-size: 125px;
  517. }
  518. .button {
  519. margin-top: 36px;
  520. }
  521. }
  522. }
  523. .steps-fade-enter-active,
  524. .steps-fade-leave-active {
  525. transition: all 0.3s ease;
  526. }
  527. .steps-fade-enter,
  528. .steps-fade-leave-to {
  529. opacity: 0;
  530. }
  531. .skip-step {
  532. background-color: var(--grey-3);
  533. color: var(--white);
  534. }
  535. </style>