Settings.vue 15 KB

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