Settings.vue 18 KB

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