Settings.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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="username">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="email">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 "../components/layout/MainHeader.vue";
  244. import MainFooter from "../components/layout/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. // prettier-ignore
  278. // eslint-disable-next-line func-names
  279. "user.username": function (value) {
  280. if (!validation.isLength(value, 2, 32)) {
  281. this.validation.username.message =
  282. "Username must have between 2 and 32 characters.";
  283. this.validation.username.valid = false;
  284. } else if (
  285. !validation.regex.azAZ09_.test(value) &&
  286. value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
  287. ) {
  288. this.validation.username.message =
  289. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.";
  290. this.validation.username.valid = false;
  291. } else {
  292. this.validation.username.message = "Everything looks great!";
  293. this.validation.username.valid = true;
  294. }
  295. },
  296. // prettier-ignore
  297. // eslint-disable-next-line func-names
  298. "user.email.address": function (value) {
  299. if (!validation.isLength(value, 3, 254)) {
  300. this.validation.email.message =
  301. "Email must have between 3 and 254 characters.";
  302. this.validation.email.valid = false;
  303. } else if (
  304. value.indexOf("@") !== value.lastIndexOf("@") ||
  305. !validation.regex.emailSimple.test(value)
  306. ) {
  307. this.validation.email.message = "Invalid Email format.";
  308. this.validation.email.valid = false;
  309. } else {
  310. this.validation.email.message = "Everything looks great!";
  311. this.validation.email.valid = true;
  312. }
  313. }
  314. },
  315. computed: mapState({
  316. userId: state => state.user.auth.userId,
  317. nightmode: state => state.user.preferences.nightmode
  318. }),
  319. mounted() {
  320. if (this.$route.hash === "") {
  321. this.$router.push("#profile");
  322. } else {
  323. this.activeTab = this.$route.hash.replace("#", "");
  324. this.localNightmode = this.nightmode;
  325. lofig.get("serverDomain").then(serverDomain => {
  326. this.serverDomain = serverDomain;
  327. });
  328. io.getSocket(socket => {
  329. this.socket = socket;
  330. this.socket.emit("users.findBySession", res => {
  331. if (res.status === "success") {
  332. this.user = res.data;
  333. this.originalUser = JSON.parse(
  334. JSON.stringify(this.user)
  335. );
  336. this.password = this.user.password;
  337. this.github = this.user.github;
  338. } else {
  339. new Toast({
  340. content: "Your are currently not signed in",
  341. timeout: 3000
  342. });
  343. }
  344. });
  345. this.socket.on("event:user.linkPassword", () => {
  346. this.password = true;
  347. });
  348. this.socket.on("event:user.linkGitHub", () => {
  349. this.github = true;
  350. });
  351. this.socket.on("event:user.unlinkPassword", () => {
  352. this.password = false;
  353. });
  354. this.socket.on("event:user.unlinkGitHub", () => {
  355. this.github = false;
  356. });
  357. });
  358. }
  359. },
  360. methods: {
  361. onInputBlur(inputName) {
  362. this.validation[inputName].entered = true;
  363. },
  364. saveChangesToProfile() {
  365. if (this.user.name !== this.originalUser.name) this.changeName();
  366. if (this.user.location !== this.originalUser.location)
  367. this.changeLocation();
  368. if (this.user.bio !== this.originalUser.bio) this.changeBio();
  369. if (this.user.avatar.type !== this.originalUser.avatar.type)
  370. this.changeAvatarType();
  371. },
  372. saveChangesToAccount() {
  373. if (this.user.username !== this.originalUser.username)
  374. this.changeUsername();
  375. if (this.user.email.address !== this.originalUser.email.address)
  376. this.changeEmail();
  377. },
  378. saveChangesPreferences() {
  379. if (this.localNightmode !== this.nightmode)
  380. this.changeNightmodeLocal();
  381. },
  382. changeEmail() {
  383. const email = this.user.email.address;
  384. if (!validation.isLength(email, 3, 254))
  385. return new Toast({
  386. content: "Email must have between 3 and 254 characters.",
  387. timeout: 8000
  388. });
  389. if (
  390. email.indexOf("@") !== email.lastIndexOf("@") ||
  391. !validation.regex.emailSimple.test(email)
  392. )
  393. return new Toast({
  394. content: "Invalid email format.",
  395. timeout: 8000
  396. });
  397. return this.socket.emit(
  398. "users.updateEmail",
  399. this.userId,
  400. email,
  401. res => {
  402. if (res.status !== "success")
  403. new Toast({ content: res.message, timeout: 8000 });
  404. else {
  405. new Toast({
  406. content: "Successfully changed email address",
  407. timeout: 4000
  408. });
  409. this.originalUser.email.address = email;
  410. }
  411. }
  412. );
  413. },
  414. changeUsername() {
  415. const { username } = this.user;
  416. if (!validation.isLength(username, 2, 32))
  417. return new Toast({
  418. content: "Username must have between 2 and 32 characters.",
  419. timeout: 8000
  420. });
  421. if (!validation.regex.azAZ09_.test(username))
  422. return new Toast({
  423. content:
  424. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  425. timeout: 8000
  426. });
  427. return this.socket.emit(
  428. "users.updateUsername",
  429. this.userId,
  430. username,
  431. res => {
  432. if (res.status !== "success")
  433. new Toast({ content: res.message, timeout: 8000 });
  434. else {
  435. new Toast({
  436. content: "Successfully changed username",
  437. timeout: 4000
  438. });
  439. this.originalUser.username = username;
  440. }
  441. }
  442. );
  443. },
  444. changeName() {
  445. const { name } = this.user;
  446. if (!validation.isLength(name, 1, 64))
  447. return new Toast({
  448. content: "Name must have between 1 and 64 characters.",
  449. timeout: 8000
  450. });
  451. return this.socket.emit(
  452. "users.updateName",
  453. this.userId,
  454. name,
  455. res => {
  456. if (res.status !== "success")
  457. new Toast({ content: res.message, timeout: 8000 });
  458. else {
  459. new Toast({
  460. content: "Successfully changed name",
  461. timeout: 4000
  462. });
  463. this.originalUser.name = name;
  464. }
  465. }
  466. );
  467. },
  468. changeLocation() {
  469. const { location } = this.user;
  470. if (!validation.isLength(location, 0, 50))
  471. return new Toast({
  472. content: "Location must have between 0 and 50 characters.",
  473. timeout: 8000
  474. });
  475. return this.socket.emit(
  476. "users.updateLocation",
  477. this.userId,
  478. location,
  479. res => {
  480. if (res.status !== "success")
  481. new Toast({ content: res.message, timeout: 8000 });
  482. else {
  483. new Toast({
  484. content: "Successfully changed location",
  485. timeout: 4000
  486. });
  487. this.originalUser.location = location;
  488. }
  489. }
  490. );
  491. },
  492. changeBio() {
  493. const { bio } = this.user;
  494. if (!validation.isLength(bio, 0, 200))
  495. return new Toast({
  496. content: "Bio must have between 0 and 200 characters.",
  497. timeout: 8000
  498. });
  499. return this.socket.emit(
  500. "users.updateBio",
  501. this.userId,
  502. bio,
  503. res => {
  504. if (res.status !== "success")
  505. new Toast({ content: res.message, timeout: 8000 });
  506. else {
  507. new Toast({
  508. content: "Successfully changed bio",
  509. timeout: 4000
  510. });
  511. this.originalUser.bio = bio;
  512. }
  513. }
  514. );
  515. },
  516. changeAvatarType() {
  517. const { type } = this.user.avatar;
  518. return this.socket.emit(
  519. "users.updateAvatarType",
  520. this.userId,
  521. type,
  522. res => {
  523. if (res.status !== "success")
  524. new Toast({ content: res.message, timeout: 8000 });
  525. else {
  526. new Toast({
  527. content: "Successfully updated avatar type",
  528. timeout: 4000
  529. });
  530. this.originalUser.avatar.type = type;
  531. }
  532. }
  533. );
  534. },
  535. changePassword() {
  536. const { newPassword } = this;
  537. if (!validation.isLength(newPassword, 6, 200))
  538. return new Toast({
  539. content: "Password must have between 6 and 200 characters.",
  540. timeout: 8000
  541. });
  542. if (!validation.regex.password.test(newPassword))
  543. return new Toast({
  544. content:
  545. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  546. timeout: 8000
  547. });
  548. return this.socket.emit(
  549. "users.updatePassword",
  550. newPassword,
  551. res => {
  552. if (res.status !== "success")
  553. new Toast({ content: res.message, timeout: 8000 });
  554. else
  555. new Toast({
  556. content: "Successfully changed password",
  557. timeout: 4000
  558. });
  559. }
  560. );
  561. },
  562. requestPassword() {
  563. return this.socket.emit("users.requestPassword", res => {
  564. new Toast({ content: res.message, timeout: 8000 });
  565. if (res.status === "success") {
  566. this.passwordStep = 2;
  567. }
  568. });
  569. },
  570. verifyCode() {
  571. if (!this.passwordCode)
  572. return new Toast({
  573. content: "Code cannot be empty",
  574. timeout: 8000
  575. });
  576. return this.socket.emit(
  577. "users.verifyPasswordCode",
  578. this.passwordCode,
  579. res => {
  580. new Toast({ content: res.message, timeout: 8000 });
  581. if (res.status === "success") {
  582. this.passwordStep = 3;
  583. }
  584. }
  585. );
  586. },
  587. setPassword() {
  588. const newPassword = this.setNewPassword;
  589. if (!validation.isLength(newPassword, 6, 200))
  590. return new Toast({
  591. content: "Password must have between 6 and 200 characters.",
  592. timeout: 8000
  593. });
  594. if (!validation.regex.password.test(newPassword))
  595. return new Toast({
  596. content:
  597. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  598. timeout: 8000
  599. });
  600. return this.socket.emit(
  601. "users.changePasswordWithCode",
  602. this.passwordCode,
  603. newPassword,
  604. res => {
  605. new Toast({ content: res.message, timeout: 8000 });
  606. }
  607. );
  608. },
  609. unlinkPassword() {
  610. this.socket.emit("users.unlinkPassword", res => {
  611. new Toast({ content: res.message, timeout: 8000 });
  612. });
  613. },
  614. unlinkGitHub() {
  615. this.socket.emit("users.unlinkGitHub", res => {
  616. new Toast({ content: res.message, timeout: 8000 });
  617. });
  618. },
  619. removeSessions() {
  620. this.socket.emit(`users.removeSessions`, this.userId, res => {
  621. new Toast({ content: res.message, timeout: 4000 });
  622. });
  623. },
  624. changeNightmodeLocal() {
  625. localStorage.setItem("nightmode", this.localNightmode);
  626. this.changeNightmode(this.localNightmode);
  627. },
  628. ...mapActions("user/preferences", ["changeNightmode"])
  629. }
  630. };
  631. </script>
  632. <style lang="scss" scoped>
  633. @import "../styles/global.scss";
  634. .container {
  635. @media only screen and (min-width: 900px) {
  636. width: 962px;
  637. margin: 0 auto;
  638. flex-direction: row;
  639. .content {
  640. width: 600px;
  641. margin-top: 0px;
  642. }
  643. }
  644. margin-top: 32px;
  645. padding: 24px;
  646. display: flex;
  647. flex-direction: column;
  648. .nav-links {
  649. height: 100%;
  650. width: 250px;
  651. margin-right: 64px;
  652. a {
  653. outline: none;
  654. border: none;
  655. box-shadow: none;
  656. color: $musare-blue;
  657. font-size: 22px;
  658. line-height: 26px;
  659. padding: 7px 0 7px 12px;
  660. width: 100%;
  661. text-align: left;
  662. cursor: pointer;
  663. border-radius: 5px;
  664. background-color: transparent;
  665. display: inline-block;
  666. &.active {
  667. color: $white;
  668. background-color: $musare-blue;
  669. }
  670. }
  671. }
  672. .content {
  673. margin: 24px 0;
  674. label {
  675. font-size: 14px;
  676. color: $dark-grey-2;
  677. padding-bottom: 4px;
  678. }
  679. input {
  680. height: 32px;
  681. }
  682. textarea {
  683. height: 96px;
  684. }
  685. input,
  686. textarea {
  687. border-radius: 3px;
  688. border: 1px solid $light-grey-2;
  689. }
  690. button {
  691. width: 100%;
  692. }
  693. .checkbox-control {
  694. input[type="checkbox"] {
  695. opacity: 0;
  696. position: absolute;
  697. }
  698. label {
  699. display: flex;
  700. flex-direction: row;
  701. align-items: center;
  702. span {
  703. cursor: pointer;
  704. width: 24px;
  705. height: 24px;
  706. background-color: $white;
  707. display: inline-block;
  708. border: 1px solid $dark-grey-2;
  709. position: relative;
  710. border-radius: 3px;
  711. }
  712. p {
  713. margin-left: 10px;
  714. }
  715. }
  716. input[type="checkbox"]:checked + label span::after {
  717. content: "";
  718. width: 18px;
  719. height: 18px;
  720. left: 2px;
  721. top: 2px;
  722. border-radius: 3px;
  723. background-color: $musare-blue;
  724. position: absolute;
  725. }
  726. }
  727. }
  728. }
  729. .avatar-select {
  730. display: flex;
  731. flex-direction: column;
  732. align-items: flex-start;
  733. .select:after {
  734. border-color: $musare-blue;
  735. }
  736. }
  737. .night-mode {
  738. label {
  739. color: #ddd !important;
  740. }
  741. }
  742. </style>