Settings.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <template>
  2. <div>
  3. <metadata title="Settings" />
  4. <main-header />
  5. <div class="container">
  6. <div class="buttons">
  7. <button
  8. :class="{ active: activeTab === 'profile' }"
  9. @click="switchTab('profile')"
  10. >
  11. Profile
  12. </button>
  13. <button
  14. :class="{ active: activeTab === 'account' }"
  15. @click="switchTab('account')"
  16. >
  17. Account
  18. </button>
  19. <button
  20. :class="{ active: activeTab === 'security' }"
  21. @click="switchTab('security')"
  22. >
  23. Security
  24. </button>
  25. <button
  26. :class="{ active: activeTab === 'preferences' }"
  27. @click="switchTab('preferences')"
  28. >
  29. Preferences
  30. </button>
  31. </div>
  32. <div class="content profile-tab" v-if="activeTab === 'profile'">
  33. <p class="control is-expanded">
  34. <label for="name">Name</label>
  35. <input
  36. class="input"
  37. id="name"
  38. type="text"
  39. placeholder="Name"
  40. v-model="user.name"
  41. />
  42. </p>
  43. <p class="control is-expanded">
  44. <label for="location">Location</label>
  45. <input
  46. class="input"
  47. id="location"
  48. type="text"
  49. placeholder="Location"
  50. v-model="user.location"
  51. />
  52. </p>
  53. <p class="control is-expanded">
  54. <label for="bio">Bio</label>
  55. <textarea
  56. class="textarea"
  57. id="bio"
  58. placeholder="Bio"
  59. v-model="user.bio"
  60. />
  61. </p>
  62. <div class="control is-expanded avatar-select">
  63. <label>Avatar</label>
  64. <div class="select">
  65. <select v-model="user.avatar.type">
  66. <option value="gravatar">Using Gravatar</option>
  67. <option value="initials">Based on initials</option>
  68. </select>
  69. </div>
  70. </div>
  71. <button
  72. class="button is-primary"
  73. @click="saveChangesToProfile()"
  74. >
  75. Save changes
  76. </button>
  77. </div>
  78. <div class="content account-tab" v-if="activeTab === 'account'">
  79. <p class="control is-expanded">
  80. <label for="name">Username</label>
  81. <input
  82. class="input"
  83. id="username"
  84. type="text"
  85. placeholder="Username"
  86. v-model="user.username"
  87. />
  88. </p>
  89. <p class="control is-expanded">
  90. <label for="location">Email</label>
  91. <input
  92. class="input"
  93. id="email"
  94. type="text"
  95. placeholder="Email"
  96. v-model="user.email.address"
  97. />
  98. </p>
  99. <button
  100. class="button is-primary"
  101. @click="saveChangesToAccount()"
  102. >
  103. Save changes
  104. </button>
  105. </div>
  106. <div class="content security-tab" v-if="activeTab === 'security'">
  107. <label v-if="!password" class="label">Add password</label>
  108. <div v-if="!password" class="control is-grouped">
  109. <button
  110. v-if="passwordStep === 1"
  111. class="button is-success"
  112. @click="requestPassword()"
  113. >
  114. Request password email
  115. </button>
  116. <br />
  117. <p
  118. v-if="passwordStep === 2"
  119. class="control is-expanded has-icon has-icon-right"
  120. >
  121. <input
  122. v-model="passwordCode"
  123. class="input"
  124. type="text"
  125. placeholder="Code"
  126. />
  127. </p>
  128. <p v-if="passwordStep === 2" class="control is-expanded">
  129. <button
  130. class="button is-success"
  131. v-on:click="verifyCode()"
  132. >
  133. Verify code
  134. </button>
  135. </p>
  136. <p
  137. v-if="passwordStep === 3"
  138. class="control is-expanded has-icon has-icon-right"
  139. >
  140. <input
  141. v-model="setNewPassword"
  142. class="input"
  143. type="password"
  144. placeholder="New password"
  145. />
  146. </p>
  147. <p v-if="passwordStep === 3" class="control is-expanded">
  148. <button
  149. class="button is-success"
  150. @click="setPassword()"
  151. >
  152. Set password
  153. </button>
  154. </p>
  155. </div>
  156. <a
  157. v-if="passwordStep === 1 && !password"
  158. href="#"
  159. @click="passwordStep = 2"
  160. >Skip this step</a
  161. >
  162. <a
  163. v-if="!github"
  164. class="button is-github"
  165. :href="`${serverDomain}/auth/github/link`"
  166. >
  167. <div class="icon">
  168. <img class="invert" src="/assets/social/github.svg" />
  169. </div>
  170. &nbsp; Link GitHub to account
  171. </a>
  172. <button
  173. v-if="password && github"
  174. class="button is-danger"
  175. @click="unlinkPassword()"
  176. >
  177. Remove logging in with password
  178. </button>
  179. <button
  180. v-if="password && github"
  181. class="button is-danger"
  182. @click="unlinkGitHub()"
  183. >
  184. Remove logging in with GitHub
  185. </button>
  186. <br />
  187. <button
  188. class="button is-warning"
  189. style="margin-top: 30px;"
  190. @click="removeSessions()"
  191. >
  192. Log out everywhere
  193. </button>
  194. </div>
  195. <div
  196. class="content preferences-tab"
  197. v-if="activeTab === 'preferences'"
  198. >
  199. <p class="control is-expanded checkbox-control">
  200. <input
  201. type="checkbox"
  202. id="nightmode"
  203. v-model="localNightmode"
  204. />
  205. <label for="nightmode">
  206. <p>Use nightmode</p>
  207. <span></span>
  208. </label>
  209. </p>
  210. <button
  211. class="button is-primary"
  212. @click="saveChangesPreferences()"
  213. >
  214. Save changes
  215. </button>
  216. </div>
  217. </div>
  218. <main-footer />
  219. </div>
  220. </template>
  221. <script>
  222. import { mapState, mapActions } from "vuex";
  223. import Toast from "toasters";
  224. import MainHeader from "../MainHeader.vue";
  225. import MainFooter from "../MainFooter.vue";
  226. import io from "../../io";
  227. import validation from "../../validation";
  228. export default {
  229. components: { MainHeader, MainFooter },
  230. data() {
  231. return {
  232. user: {},
  233. newPassword: "",
  234. password: false,
  235. github: false,
  236. setNewPassword: "",
  237. passwordStep: 1,
  238. passwordCode: "",
  239. serverDomain: "",
  240. activeTab: "profile",
  241. localNightmode: false
  242. };
  243. },
  244. computed: mapState({
  245. userId: state => state.user.auth.userId,
  246. nightmode: state => state.user.preferences.nightmode
  247. }),
  248. mounted() {
  249. this.localNightmode = this.nightmode;
  250. lofig.get("serverDomain").then(serverDomain => {
  251. this.serverDomain = serverDomain;
  252. });
  253. io.getSocket(socket => {
  254. this.socket = socket;
  255. this.socket.emit("users.findBySession", res => {
  256. if (res.status === "success") {
  257. this.user = res.data;
  258. this.password = this.user.password;
  259. this.github = this.user.github;
  260. } else {
  261. new Toast({
  262. content: "Your are currently not signed in",
  263. timeout: 3000
  264. });
  265. }
  266. });
  267. this.socket.on("event:user.linkPassword", () => {
  268. this.password = true;
  269. });
  270. this.socket.on("event:user.linkGitHub", () => {
  271. this.github = true;
  272. });
  273. this.socket.on("event:user.unlinkPassword", () => {
  274. this.password = false;
  275. });
  276. this.socket.on("event:user.unlinkGitHub", () => {
  277. this.github = false;
  278. });
  279. });
  280. },
  281. methods: {
  282. switchTab(tabName) {
  283. this.activeTab = tabName;
  284. },
  285. saveChangesToProfile() {
  286. this.changeName();
  287. this.changeLocation();
  288. this.changeBio();
  289. this.changeAvatarType();
  290. },
  291. saveChangesToAccount() {
  292. this.changeUsername();
  293. this.changeEmail();
  294. },
  295. saveChangesPreferences() {
  296. this.changeNightmodeLocal();
  297. },
  298. changeEmail() {
  299. const email = this.user.email.address;
  300. if (!validation.isLength(email, 3, 254))
  301. return new Toast({
  302. content: "Email must have between 3 and 254 characters.",
  303. timeout: 8000
  304. });
  305. if (
  306. email.indexOf("@") !== email.lastIndexOf("@") ||
  307. !validation.regex.emailSimple.test(email)
  308. )
  309. return new Toast({
  310. content: "Invalid email format.",
  311. timeout: 8000
  312. });
  313. return this.socket.emit(
  314. "users.updateEmail",
  315. this.userId,
  316. email,
  317. res => {
  318. if (res.status !== "success")
  319. new Toast({ content: res.message, timeout: 8000 });
  320. else
  321. new Toast({
  322. content: "Successfully changed email address",
  323. timeout: 4000
  324. });
  325. }
  326. );
  327. },
  328. changeUsername() {
  329. const { username } = this.user;
  330. if (!validation.isLength(username, 2, 32))
  331. return new Toast({
  332. content: "Username must have between 2 and 32 characters.",
  333. timeout: 8000
  334. });
  335. if (!validation.regex.azAZ09_.test(username))
  336. return new Toast({
  337. content:
  338. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  339. timeout: 8000
  340. });
  341. return this.socket.emit(
  342. "users.updateUsername",
  343. this.userId,
  344. username,
  345. res => {
  346. if (res.status !== "success")
  347. new Toast({ content: res.message, timeout: 8000 });
  348. else
  349. new Toast({
  350. content: "Successfully changed username",
  351. timeout: 4000
  352. });
  353. }
  354. );
  355. },
  356. changeName() {
  357. const { name } = this.user;
  358. if (!validation.isLength(name, 1, 64))
  359. return new Toast({
  360. content: "Name must have between 1 and 64 characters.",
  361. timeout: 8000
  362. });
  363. return this.socket.emit(
  364. "users.updateName",
  365. this.userId,
  366. name,
  367. res => {
  368. if (res.status !== "success")
  369. new Toast({ content: res.message, timeout: 8000 });
  370. else
  371. new Toast({
  372. content: "Successfully changed name",
  373. timeout: 4000
  374. });
  375. }
  376. );
  377. },
  378. changeLocation() {
  379. const { location } = this.user;
  380. if (!validation.isLength(location, 0, 50))
  381. return new Toast({
  382. content: "Location must have between 0 and 50 characters.",
  383. timeout: 8000
  384. });
  385. return this.socket.emit(
  386. "users.updateLocation",
  387. this.userId,
  388. location,
  389. res => {
  390. if (res.status !== "success")
  391. new Toast({ content: res.message, timeout: 8000 });
  392. else
  393. new Toast({
  394. content: "Successfully changed location",
  395. timeout: 4000
  396. });
  397. }
  398. );
  399. },
  400. changeBio() {
  401. const { bio } = this.user;
  402. if (!validation.isLength(bio, 0, 200))
  403. return new Toast({
  404. content: "Bio must have between 0 and 200 characters.",
  405. timeout: 8000
  406. });
  407. return this.socket.emit(
  408. "users.updateBio",
  409. this.userId,
  410. bio,
  411. res => {
  412. if (res.status !== "success")
  413. new Toast({ content: res.message, timeout: 8000 });
  414. else
  415. new Toast({
  416. content: "Successfully changed bio",
  417. timeout: 4000
  418. });
  419. }
  420. );
  421. },
  422. changeAvatarType() {
  423. const { type } = this.user.avatar;
  424. return this.socket.emit(
  425. "users.updateAvatarType",
  426. this.userId,
  427. type,
  428. res => {
  429. new Toast({ content: res.message, timeout: 8000 });
  430. }
  431. );
  432. },
  433. changePassword() {
  434. const { newPassword } = this;
  435. if (!validation.isLength(newPassword, 6, 200))
  436. return new Toast({
  437. content: "Password must have between 6 and 200 characters.",
  438. timeout: 8000
  439. });
  440. if (!validation.regex.password.test(newPassword))
  441. return new Toast({
  442. content:
  443. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  444. timeout: 8000
  445. });
  446. return this.socket.emit(
  447. "users.updatePassword",
  448. newPassword,
  449. res => {
  450. if (res.status !== "success")
  451. new Toast({ content: res.message, timeout: 8000 });
  452. else
  453. new Toast({
  454. content: "Successfully changed password",
  455. timeout: 4000
  456. });
  457. }
  458. );
  459. },
  460. requestPassword() {
  461. return this.socket.emit("users.requestPassword", res => {
  462. new Toast({ content: res.message, timeout: 8000 });
  463. if (res.status === "success") {
  464. this.passwordStep = 2;
  465. }
  466. });
  467. },
  468. verifyCode() {
  469. if (!this.passwordCode)
  470. return new Toast({
  471. content: "Code cannot be empty",
  472. timeout: 8000
  473. });
  474. return this.socket.emit(
  475. "users.verifyPasswordCode",
  476. this.passwordCode,
  477. res => {
  478. new Toast({ content: res.message, timeout: 8000 });
  479. if (res.status === "success") {
  480. this.passwordStep = 3;
  481. }
  482. }
  483. );
  484. },
  485. setPassword() {
  486. const newPassword = this.setNewPassword;
  487. if (!validation.isLength(newPassword, 6, 200))
  488. return new Toast({
  489. content: "Password must have between 6 and 200 characters.",
  490. timeout: 8000
  491. });
  492. if (!validation.regex.password.test(newPassword))
  493. return new Toast({
  494. content:
  495. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  496. timeout: 8000
  497. });
  498. return this.socket.emit(
  499. "users.changePasswordWithCode",
  500. this.passwordCode,
  501. newPassword,
  502. res => {
  503. new Toast({ content: res.message, timeout: 8000 });
  504. }
  505. );
  506. },
  507. unlinkPassword() {
  508. this.socket.emit("users.unlinkPassword", res => {
  509. new Toast({ content: res.message, timeout: 8000 });
  510. });
  511. },
  512. unlinkGitHub() {
  513. this.socket.emit("users.unlinkGitHub", res => {
  514. new Toast({ content: res.message, timeout: 8000 });
  515. });
  516. },
  517. removeSessions() {
  518. this.socket.emit(`users.removeSessions`, this.userId, res => {
  519. new Toast({ content: res.message, timeout: 4000 });
  520. });
  521. },
  522. changeNightmodeLocal() {
  523. localStorage.setItem("nightmode", this.localNightmode);
  524. this.changeNightmode(this.localNightmode);
  525. },
  526. ...mapActions("user/preferences", ["changeNightmode"])
  527. }
  528. };
  529. </script>
  530. <style lang="scss" scoped>
  531. @import "styles/global.scss";
  532. .container {
  533. width: 962px;
  534. margin-left: auto;
  535. margin-right: auto;
  536. margin-top: 32px;
  537. padding: 24px;
  538. display: flex;
  539. .buttons {
  540. height: 100%;
  541. width: 250px;
  542. margin-right: 64px;
  543. button {
  544. outline: none;
  545. border: none;
  546. box-shadow: none;
  547. color: $musareBlue;
  548. font-size: 22px;
  549. line-height: 26px;
  550. padding: 7px 0 7px 12px;
  551. width: 100%;
  552. text-align: left;
  553. cursor: pointer;
  554. border-radius: 5px;
  555. background-color: transparent;
  556. &.active {
  557. color: $white;
  558. background-color: $musareBlue;
  559. }
  560. }
  561. }
  562. .content {
  563. width: 600px;
  564. .control {
  565. margin-bottom: 24px;
  566. }
  567. label {
  568. font-size: 14px;
  569. color: $dark-grey-2;
  570. padding-bottom: 4px;
  571. }
  572. input {
  573. height: 32px;
  574. }
  575. textarea {
  576. height: 96px;
  577. }
  578. input,
  579. textarea {
  580. border-radius: 3px;
  581. border: 1px solid $light-grey-2;
  582. }
  583. button {
  584. width: 100%;
  585. }
  586. .checkbox-control {
  587. input[type="checkbox"] {
  588. opacity: 0;
  589. position: absolute;
  590. }
  591. label span {
  592. cursor: pointer;
  593. width: 24px;
  594. height: 24px;
  595. background-color: $white;
  596. display: inline-block;
  597. border: 1px solid $dark-grey-2;
  598. position: relative;
  599. border-radius: 3px;
  600. }
  601. label p {
  602. margin-bottom: 4px;
  603. }
  604. input[type="checkbox"]:checked + label span::after {
  605. content: "";
  606. width: 18px;
  607. height: 18px;
  608. left: 2px;
  609. top: 2px;
  610. border-radius: 3px;
  611. background-color: $musareBlue;
  612. position: absolute;
  613. }
  614. }
  615. }
  616. }
  617. .avatar-select {
  618. display: flex;
  619. flex-direction: column;
  620. align-items: flex-start;
  621. .select:after {
  622. border-color: $musareBlue;
  623. }
  624. }
  625. .night-mode {
  626. label {
  627. color: #ddd !important;
  628. }
  629. }
  630. </style>