ResetPassword.vue 12 KB

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