Settings.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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-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-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. <span></span>
  207. <p>Use nightmode</p>
  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. originalUser: {},
  234. newPassword: "",
  235. password: false,
  236. github: false,
  237. setNewPassword: "",
  238. passwordStep: 1,
  239. passwordCode: "",
  240. serverDomain: "",
  241. activeTab: "profile",
  242. localNightmode: false
  243. };
  244. },
  245. computed: mapState({
  246. userId: state => state.user.auth.userId,
  247. nightmode: state => state.user.preferences.nightmode
  248. }),
  249. mounted() {
  250. this.localNightmode = this.nightmode;
  251. lofig.get("serverDomain").then(serverDomain => {
  252. this.serverDomain = serverDomain;
  253. });
  254. io.getSocket(socket => {
  255. this.socket = socket;
  256. this.socket.emit("users.findBySession", res => {
  257. if (res.status === "success") {
  258. this.user = res.data;
  259. this.originalUser = JSON.parse(JSON.stringify(this.user));
  260. this.password = this.user.password;
  261. this.github = this.user.github;
  262. } else {
  263. new Toast({
  264. content: "Your are currently not signed in",
  265. timeout: 3000
  266. });
  267. }
  268. });
  269. this.socket.on("event:user.linkPassword", () => {
  270. this.password = true;
  271. });
  272. this.socket.on("event:user.linkGitHub", () => {
  273. this.github = true;
  274. });
  275. this.socket.on("event:user.unlinkPassword", () => {
  276. this.password = false;
  277. });
  278. this.socket.on("event:user.unlinkGitHub", () => {
  279. this.github = false;
  280. });
  281. });
  282. },
  283. methods: {
  284. switchTab(tabName) {
  285. this.activeTab = tabName;
  286. },
  287. saveChangesToProfile() {
  288. if (this.user.name !== this.originalUser.name) this.changeName();
  289. if (this.user.location !== this.originalUser.location)
  290. this.changeLocation();
  291. if (this.user.bio !== this.originalUser.bio) this.changeBio();
  292. if (this.user.avatar.type !== this.originalUser.avatar.type)
  293. this.changeAvatarType();
  294. },
  295. saveChangesToAccount() {
  296. if (this.user.username !== this.originalUser.username) this.changeUsername();
  297. if (this.user.email.address !== this.originalUser.email.address) this.changeEmail();
  298. },
  299. saveChangesPreferences() {
  300. if (this.localNightmode !== this.nightmode) this.changeNightmodeLocal();
  301. },
  302. changeEmail() {
  303. const email = this.user.email.address;
  304. if (!validation.isLength(email, 3, 254))
  305. return new Toast({
  306. content: "Email must have between 3 and 254 characters.",
  307. timeout: 8000
  308. });
  309. if (
  310. email.indexOf("@") !== email.lastIndexOf("@") ||
  311. !validation.regex.emailSimple.test(email)
  312. )
  313. return new Toast({
  314. content: "Invalid email format.",
  315. timeout: 8000
  316. });
  317. return this.socket.emit(
  318. "users.updateEmail",
  319. this.userId,
  320. email,
  321. res => {
  322. if (res.status !== "success")
  323. new Toast({ content: res.message, timeout: 8000 });
  324. else {
  325. new Toast({
  326. content: "Successfully changed email address",
  327. timeout: 4000
  328. });
  329. this.originalUser.email.address = email;
  330. }
  331. }
  332. );
  333. },
  334. changeUsername() {
  335. const { username } = this.user;
  336. if (!validation.isLength(username, 2, 32))
  337. return new Toast({
  338. content: "Username must have between 2 and 32 characters.",
  339. timeout: 8000
  340. });
  341. if (!validation.regex.azAZ09_.test(username))
  342. return new Toast({
  343. content:
  344. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  345. timeout: 8000
  346. });
  347. return this.socket.emit(
  348. "users.updateUsername",
  349. this.userId,
  350. username,
  351. res => {
  352. if (res.status !== "success")
  353. new Toast({ content: res.message, timeout: 8000 });
  354. else {
  355. new Toast({
  356. content: "Successfully changed username",
  357. timeout: 4000
  358. });
  359. this.originalUser.username = username;
  360. }
  361. }
  362. );
  363. },
  364. changeName() {
  365. const { name } = this.user;
  366. if (!validation.isLength(name, 1, 64))
  367. return new Toast({
  368. content: "Name must have between 1 and 64 characters.",
  369. timeout: 8000
  370. });
  371. return this.socket.emit(
  372. "users.updateName",
  373. this.userId,
  374. name,
  375. res => {
  376. if (res.status !== "success")
  377. new Toast({ content: res.message, timeout: 8000 });
  378. else {
  379. new Toast({
  380. content: "Successfully changed name",
  381. timeout: 4000
  382. });
  383. this.originalUser.name = name;
  384. }
  385. }
  386. );
  387. },
  388. changeLocation() {
  389. const { location } = this.user;
  390. if (!validation.isLength(location, 0, 50))
  391. return new Toast({
  392. content: "Location must have between 0 and 50 characters.",
  393. timeout: 8000
  394. });
  395. return this.socket.emit(
  396. "users.updateLocation",
  397. this.userId,
  398. location,
  399. res => {
  400. if (res.status !== "success")
  401. new Toast({ content: res.message, timeout: 8000 });
  402. else {
  403. new Toast({
  404. content: "Successfully changed location",
  405. timeout: 4000
  406. });
  407. this.originalUser.location = location;
  408. }
  409. }
  410. );
  411. },
  412. changeBio() {
  413. const { bio } = this.user;
  414. if (!validation.isLength(bio, 0, 200))
  415. return new Toast({
  416. content: "Bio must have between 0 and 200 characters.",
  417. timeout: 8000
  418. });
  419. return this.socket.emit(
  420. "users.updateBio",
  421. this.userId,
  422. bio,
  423. res => {
  424. if (res.status !== "success")
  425. new Toast({ content: res.message, timeout: 8000 });
  426. else {
  427. new Toast({
  428. content: "Successfully changed bio",
  429. timeout: 4000
  430. });
  431. this.originalUser.bio = bio;
  432. }
  433. }
  434. );
  435. },
  436. changeAvatarType() {
  437. const { type } = this.user.avatar;
  438. return this.socket.emit(
  439. "users.updateAvatarType",
  440. this.userId,
  441. type,
  442. res => {
  443. if (res.status !== "success")
  444. new Toast({ content: res.message, timeout: 8000 });
  445. else {
  446. new Toast({
  447. content: "Successfully updated avatar type",
  448. timeout: 4000
  449. });
  450. this.originalUser.avatar.type = type;
  451. }
  452. }
  453. );
  454. },
  455. changePassword() {
  456. const { newPassword } = this;
  457. if (!validation.isLength(newPassword, 6, 200))
  458. return new Toast({
  459. content: "Password must have between 6 and 200 characters.",
  460. timeout: 8000
  461. });
  462. if (!validation.regex.password.test(newPassword))
  463. return new Toast({
  464. content:
  465. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  466. timeout: 8000
  467. });
  468. return this.socket.emit(
  469. "users.updatePassword",
  470. newPassword,
  471. res => {
  472. if (res.status !== "success")
  473. new Toast({ content: res.message, timeout: 8000 });
  474. else
  475. new Toast({
  476. content: "Successfully changed password",
  477. timeout: 4000
  478. });
  479. }
  480. );
  481. },
  482. requestPassword() {
  483. return this.socket.emit("users.requestPassword", res => {
  484. new Toast({ content: res.message, timeout: 8000 });
  485. if (res.status === "success") {
  486. this.passwordStep = 2;
  487. }
  488. });
  489. },
  490. verifyCode() {
  491. if (!this.passwordCode)
  492. return new Toast({
  493. content: "Code cannot be empty",
  494. timeout: 8000
  495. });
  496. return this.socket.emit(
  497. "users.verifyPasswordCode",
  498. this.passwordCode,
  499. res => {
  500. new Toast({ content: res.message, timeout: 8000 });
  501. if (res.status === "success") {
  502. this.passwordStep = 3;
  503. }
  504. }
  505. );
  506. },
  507. setPassword() {
  508. const newPassword = this.setNewPassword;
  509. if (!validation.isLength(newPassword, 6, 200))
  510. return new Toast({
  511. content: "Password must have between 6 and 200 characters.",
  512. timeout: 8000
  513. });
  514. if (!validation.regex.password.test(newPassword))
  515. return new Toast({
  516. content:
  517. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  518. timeout: 8000
  519. });
  520. return this.socket.emit(
  521. "users.changePasswordWithCode",
  522. this.passwordCode,
  523. newPassword,
  524. res => {
  525. new Toast({ content: res.message, timeout: 8000 });
  526. }
  527. );
  528. },
  529. unlinkPassword() {
  530. this.socket.emit("users.unlinkPassword", res => {
  531. new Toast({ content: res.message, timeout: 8000 });
  532. });
  533. },
  534. unlinkGitHub() {
  535. this.socket.emit("users.unlinkGitHub", res => {
  536. new Toast({ content: res.message, timeout: 8000 });
  537. });
  538. },
  539. removeSessions() {
  540. this.socket.emit(`users.removeSessions`, this.userId, res => {
  541. new Toast({ content: res.message, timeout: 4000 });
  542. });
  543. },
  544. changeNightmodeLocal() {
  545. localStorage.setItem("nightmode", this.localNightmode);
  546. this.changeNightmode(this.localNightmode);
  547. },
  548. ...mapActions("user/preferences", ["changeNightmode"])
  549. }
  550. };
  551. </script>
  552. <style lang="scss" scoped>
  553. @import "styles/global.scss";
  554. .container {
  555. width: 962px;
  556. margin-left: auto;
  557. margin-right: auto;
  558. margin-top: 32px;
  559. padding: 24px;
  560. display: flex;
  561. .buttons {
  562. height: 100%;
  563. width: 250px;
  564. margin-right: 64px;
  565. button {
  566. outline: none;
  567. border: none;
  568. box-shadow: none;
  569. color: $musareBlue;
  570. font-size: 22px;
  571. line-height: 26px;
  572. padding: 7px 0 7px 12px;
  573. width: 100%;
  574. text-align: left;
  575. cursor: pointer;
  576. border-radius: 5px;
  577. background-color: transparent;
  578. &.active {
  579. color: $white;
  580. background-color: $musareBlue;
  581. }
  582. }
  583. }
  584. .content {
  585. width: 600px;
  586. .control {
  587. margin-bottom: 24px;
  588. }
  589. label {
  590. font-size: 14px;
  591. color: $dark-grey-2;
  592. padding-bottom: 4px;
  593. }
  594. input {
  595. height: 32px;
  596. }
  597. textarea {
  598. height: 96px;
  599. }
  600. input,
  601. textarea {
  602. border-radius: 3px;
  603. border: 1px solid $light-grey-2;
  604. }
  605. button {
  606. width: 100%;
  607. }
  608. .checkbox-control {
  609. input[type="checkbox"] {
  610. opacity: 0;
  611. position: absolute;
  612. }
  613. label {
  614. display: flex;
  615. flex-direction: row;
  616. align-items: center;
  617. span {
  618. cursor: pointer;
  619. width: 24px;
  620. height: 24px;
  621. background-color: $white;
  622. display: inline-block;
  623. border: 1px solid $dark-grey-2;
  624. position: relative;
  625. border-radius: 3px;
  626. }
  627. p {
  628. margin-left: 10px;
  629. }
  630. }
  631. input[type="checkbox"]:checked + label span::after {
  632. content: "";
  633. width: 18px;
  634. height: 18px;
  635. left: 2px;
  636. top: 2px;
  637. border-radius: 3px;
  638. background-color: $musareBlue;
  639. position: absolute;
  640. }
  641. }
  642. }
  643. }
  644. .avatar-select {
  645. display: flex;
  646. flex-direction: column;
  647. align-items: flex-start;
  648. .select:after {
  649. border-color: $musareBlue;
  650. }
  651. }
  652. .night-mode {
  653. label {
  654. color: #ddd !important;
  655. }
  656. }
  657. </style>