Settings.vue 16 KB

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