Settings.vue 15 KB

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