ResetPassword.vue 13 KB

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