ResetPassword.vue 13 KB

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