ResetPassword.vue 11 KB

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