Settings.vue 10 KB

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