Settings.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <template>
  2. <div class="app">
  3. <metadata title="Settings" />
  4. <main-header />
  5. <div class="content-wrapper">
  6. <h2>Settings</h2>
  7. <div class="settingsContainer">
  8. <div class="settingsRow">
  9. <div class="setting">
  10. <label class="label">Username</label>
  11. <div class="inputArea">
  12. <input
  13. v-model="user.username"
  14. class="input"
  15. type="text"
  16. placeholder="Change username"
  17. />
  18. <button
  19. class="button is-success"
  20. @click="changeUsername()"
  21. >
  22. <i class="material-icons">
  23. save
  24. </i>
  25. </button>
  26. </div>
  27. </div>
  28. <div class="setting">
  29. <label class="label">Email</label>
  30. <div v-if="user.email" class="inputArea">
  31. <input
  32. v-model="user.email.address"
  33. class="input"
  34. type="text"
  35. placeholder="Change email address"
  36. />
  37. <button
  38. class="button is-success"
  39. @click="changeEmail()"
  40. >
  41. <i class="material-icons">
  42. save
  43. </i>
  44. </button>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="settingsRow">
  49. <div class="setting">
  50. <label class="label">Location</label>
  51. <div class="inputArea">
  52. <input
  53. v-model="user.location"
  54. class="input"
  55. type="text"
  56. placeholder="Change location"
  57. />
  58. <button
  59. class="button is-success"
  60. @click="changeLocation()"
  61. >
  62. <i class="material-icons">
  63. save
  64. </i>
  65. </button>
  66. </div>
  67. </div>
  68. <div class="setting">
  69. <label class="label">Bio</label>
  70. <div class="inputArea">
  71. <textarea
  72. v-model="user.bio"
  73. class="textarea"
  74. type="text"
  75. placeholder="Change bio"
  76. />
  77. <button
  78. class="button is-success"
  79. @click="changeBio()"
  80. >
  81. <i class="material-icons">
  82. save
  83. </i>
  84. </button>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="settingsRow">
  89. <div v-if="password" class="setting">
  90. <label class="label">Change Password</label>
  91. <div class="inputArea">
  92. <input
  93. v-model="newPassword"
  94. class="input"
  95. type="password"
  96. placeholder="Change password"
  97. />
  98. <button
  99. class="button is-success"
  100. @click="changePassword()"
  101. >
  102. <i class="material-icons">
  103. save
  104. </i>
  105. </button>
  106. </div>
  107. </div>
  108. <div v-if="!password" class="setting">
  109. <label class="label">Add password</label>
  110. <div class="inputArea">
  111. <button
  112. v-if="passwordStep === 1 && !password"
  113. href="#"
  114. class="button codeButton is-info"
  115. @click="passwordStep = 2"
  116. >
  117. Enter Code
  118. </button>
  119. <button
  120. v-if="passwordStep === 1"
  121. class="button is-success"
  122. @click="requestPassword()"
  123. >
  124. Request password email
  125. </button>
  126. <br />
  127. <input
  128. v-if="passwordStep === 2"
  129. v-model="passwordCode"
  130. class="input"
  131. type="text"
  132. placeholder="Code"
  133. />
  134. <button
  135. v-if="passwordStep === 2"
  136. class="button is-success"
  137. v-on:click="verifyCode()"
  138. >
  139. <i class="material-icons">
  140. save
  141. </i>
  142. </button>
  143. <input
  144. v-if="passwordStep === 3"
  145. v-model="setNewPassword"
  146. class="input"
  147. type="password"
  148. placeholder="New password"
  149. />
  150. <button
  151. v-if="passwordStep === 3"
  152. class="button is-success"
  153. @click="setPassword()"
  154. >
  155. <i class="material-icons">
  156. save
  157. </i>
  158. </button>
  159. </div>
  160. </div>
  161. <div class="setting buttonsOnly">
  162. <a
  163. v-if="!github"
  164. class="button is-github"
  165. :href="`${serverDomain}/auth/github/link`"
  166. >
  167. <div class="icon">
  168. <img
  169. class="invert"
  170. src="/assets/social/github.svg"
  171. />
  172. </div>
  173. &nbsp; Link GitHub to account
  174. </a>
  175. <button
  176. v-if="password && github"
  177. class="button is-danger"
  178. @click="unlinkPassword()"
  179. >
  180. Remove logging in with password
  181. </button>
  182. <button
  183. v-if="password && github"
  184. class="button is-danger"
  185. @click="unlinkGitHub()"
  186. >
  187. Remove logging in with GitHub
  188. </button>
  189. <button
  190. class="button is-warning"
  191. @click="removeSessions()"
  192. >
  193. Log out everywhere
  194. </button>
  195. </div>
  196. </div>
  197. </div>
  198. <main-footer />
  199. </div>
  200. </div>
  201. </template>
  202. <script>
  203. import { mapState } from "vuex";
  204. import Toast from "toasters";
  205. import MainHeader from "../MainHeader.vue";
  206. import MainFooter from "../MainFooter.vue";
  207. import io from "../../io";
  208. import validation from "../../validation";
  209. export default {
  210. components: { MainHeader, MainFooter },
  211. data() {
  212. return {
  213. user: {},
  214. newPassword: "",
  215. password: false,
  216. github: false,
  217. setNewPassword: "",
  218. passwordStep: 1,
  219. passwordCode: "",
  220. serverDomain: ""
  221. };
  222. },
  223. computed: mapState({
  224. userId: state => state.user.auth.userId
  225. }),
  226. mounted() {
  227. lofig.get("serverDomain").then(serverDomain => {
  228. this.serverDomain = serverDomain;
  229. });
  230. io.getSocket(socket => {
  231. this.socket = socket;
  232. this.socket.emit("users.findBySession", res => {
  233. if (res.status === "success") {
  234. this.user = res.data;
  235. this.password = this.user.password;
  236. this.github = this.user.github;
  237. } else {
  238. new Toast({
  239. content: "Your are currently not signed in",
  240. timeout: 3000
  241. });
  242. }
  243. });
  244. this.socket.on("event:user.linkPassword", () => {
  245. this.password = true;
  246. });
  247. this.socket.on("event:user.linkGitHub", () => {
  248. this.github = true;
  249. });
  250. this.socket.on("event:user.unlinkPassword", () => {
  251. this.password = false;
  252. });
  253. this.socket.on("event:user.unlinkGitHub", () => {
  254. this.github = false;
  255. });
  256. });
  257. },
  258. methods: {
  259. changeEmail() {
  260. const email = this.user.email.address;
  261. if (!validation.isLength(email, 3, 254))
  262. return new Toast({
  263. content: "Email must have between 3 and 254 characters.",
  264. timeout: 8000
  265. });
  266. if (
  267. email.indexOf("@") !== email.lastIndexOf("@") ||
  268. !validation.regex.emailSimple.test(email)
  269. )
  270. return new Toast({
  271. content: "Invalid email format.",
  272. timeout: 8000
  273. });
  274. return this.socket.emit(
  275. "users.updateEmail",
  276. this.userId,
  277. email,
  278. res => {
  279. if (res.status !== "success")
  280. new Toast({ content: res.message, timeout: 8000 });
  281. else
  282. new Toast({
  283. content: "Successfully changed email address",
  284. timeout: 4000
  285. });
  286. }
  287. );
  288. },
  289. changeUsername() {
  290. const { username } = this.user;
  291. if (!validation.isLength(username, 2, 32))
  292. return new Toast({
  293. content: "Username must have between 2 and 32 characters.",
  294. timeout: 8000
  295. });
  296. if (!validation.regex.azAZ09_.test(username))
  297. return new Toast({
  298. content:
  299. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  300. timeout: 8000
  301. });
  302. return this.socket.emit(
  303. "users.updateUsername",
  304. this.userId,
  305. username,
  306. res => {
  307. if (res.status !== "success")
  308. new Toast({ content: res.message, timeout: 8000 });
  309. else
  310. new Toast({
  311. content: "Successfully changed username",
  312. timeout: 4000
  313. });
  314. }
  315. );
  316. },
  317. changeLocation() {
  318. const { location } = this.user;
  319. if (!validation.isLength(location, 0, 50))
  320. return new Toast({
  321. content: "Location must have between 0 and 50 characters.",
  322. timeout: 8000
  323. });
  324. return this.socket.emit(
  325. "users.updateLocation",
  326. this.userId,
  327. location,
  328. res => {
  329. if (res.status !== "success")
  330. new Toast({ content: res.message, timeout: 8000 });
  331. else
  332. new Toast({
  333. content: "Successfully changed location",
  334. timeout: 4000
  335. });
  336. }
  337. );
  338. },
  339. changeBio() {
  340. const { bio } = this.user;
  341. if (!validation.isLength(bio, 0, 200))
  342. return new Toast({
  343. content: "Bio must have between 0 and 200 characters.",
  344. timeout: 8000
  345. });
  346. return this.socket.emit(
  347. "users.updateBio",
  348. this.userId,
  349. bio,
  350. res => {
  351. if (res.status !== "success")
  352. new Toast({ content: res.message, timeout: 8000 });
  353. else
  354. new Toast({
  355. content: "Successfully changed bio",
  356. timeout: 4000
  357. });
  358. }
  359. );
  360. },
  361. changePassword() {
  362. const { newPassword } = this;
  363. if (!validation.isLength(newPassword, 6, 200))
  364. return new Toast({
  365. content: "Password must have between 6 and 200 characters.",
  366. timeout: 8000
  367. });
  368. if (!validation.regex.password.test(newPassword))
  369. return new Toast({
  370. content:
  371. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  372. timeout: 8000
  373. });
  374. return this.socket.emit(
  375. "users.updatePassword",
  376. newPassword,
  377. res => {
  378. if (res.status !== "success")
  379. new Toast({ content: res.message, timeout: 8000 });
  380. else
  381. new Toast({
  382. content: "Successfully changed password",
  383. timeout: 4000
  384. });
  385. }
  386. );
  387. },
  388. requestPassword() {
  389. return this.socket.emit("users.requestPassword", res => {
  390. new Toast({ content: res.message, timeout: 8000 });
  391. if (res.status === "success") {
  392. this.passwordStep = 2;
  393. }
  394. });
  395. },
  396. verifyCode() {
  397. if (!this.passwordCode)
  398. return new Toast({
  399. content: "Code cannot be empty",
  400. timeout: 8000
  401. });
  402. return this.socket.emit(
  403. "users.verifyPasswordCode",
  404. this.passwordCode,
  405. res => {
  406. new Toast({ content: res.message, timeout: 8000 });
  407. if (res.status === "success") {
  408. this.passwordStep = 3;
  409. }
  410. }
  411. );
  412. },
  413. setPassword() {
  414. const newPassword = this.setNewPassword;
  415. if (!validation.isLength(newPassword, 6, 200))
  416. return new Toast({
  417. content: "Password must have between 6 and 200 characters.",
  418. timeout: 8000
  419. });
  420. if (!validation.regex.password.test(newPassword))
  421. return new Toast({
  422. content:
  423. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  424. timeout: 8000
  425. });
  426. return this.socket.emit(
  427. "users.changePasswordWithCode",
  428. this.passwordCode,
  429. newPassword,
  430. res => {
  431. new Toast({ content: res.message, timeout: 8000 });
  432. }
  433. );
  434. },
  435. unlinkPassword() {
  436. this.socket.emit("users.unlinkPassword", res => {
  437. new Toast({ content: res.message, timeout: 8000 });
  438. });
  439. },
  440. unlinkGitHub() {
  441. this.socket.emit("users.unlinkGitHub", res => {
  442. new Toast({ content: res.message, timeout: 8000 });
  443. });
  444. },
  445. removeSessions() {
  446. this.socket.emit(`users.removeSessions`, this.userId, res => {
  447. new Toast({ content: res.message, timeout: 4000 });
  448. });
  449. }
  450. }
  451. };
  452. </script>
  453. <style lang="scss" scoped>
  454. @import "scss/variables/colors.scss";
  455. .content-wrapper {
  456. padding: 30px 0 calc(230px + 60px) 0;
  457. }
  458. h2 {
  459. font-size: 36px;
  460. font-weight: 600;
  461. margin: 10px 0 15px 0;
  462. text-align: center;
  463. }
  464. .settingsContainer {
  465. margin: 0 auto;
  466. border: 1px solid #a3e0ff;
  467. background-color: #f4f4f4;
  468. border-radius: 5px;
  469. padding: 16px;
  470. width: 60%;
  471. .settingsRow {
  472. display: inline-flex;
  473. width: 100%;
  474. .setting {
  475. margin-top: 10px;
  476. width: 100%;
  477. .inputArea {
  478. display: flex;
  479. input {
  480. flex: 2 1 0;
  481. height: 40px;
  482. }
  483. textarea {
  484. flex: 2 1 0;
  485. min-height: 80px;
  486. min-width: inherit;
  487. }
  488. button {
  489. margin-left: 10px;
  490. &.codeButton {
  491. flex-grow: 1;
  492. margin-left: 0;
  493. margin-right: auto;
  494. }
  495. }
  496. }
  497. &:nth-child(even) {
  498. margin-left: 15px;
  499. }
  500. .button,
  501. button {
  502. width: auto;
  503. height: 40px;
  504. &.is-github {
  505. margin-left: 0;
  506. margin-top: 20px;
  507. width: 100%;
  508. }
  509. }
  510. &.buttonsOnly {
  511. margin-top: auto;
  512. button {
  513. margin-top: 10px;
  514. width: 100%;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. @media screen and (max-width: 1200px) {
  521. .settingsContainer {
  522. width: 80%;
  523. }
  524. }
  525. @media screen and (max-width: 800px) {
  526. .settingsContainer {
  527. width: 90%;
  528. .settingsRow {
  529. display: block;
  530. .setting:nth-child(even) {
  531. margin-left: auto;
  532. }
  533. }
  534. }
  535. }
  536. @media screen and (max-width: 560px) {
  537. .settingsContainer .settingsRow .setting {
  538. button,
  539. .button {
  540. margin-top: 10px;
  541. margin-left: 0 !important;
  542. width: 100% !important;
  543. height: 40px;
  544. }
  545. .inputArea {
  546. display: block;
  547. input {
  548. height: 40px;
  549. }
  550. }
  551. }
  552. }
  553. a {
  554. color: $primary-color !important;
  555. }
  556. </style>