Settings.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div>
  3. <metadata title="Settings" />
  4. <main-header />
  5. <div class="container">
  6. <!--Implement Validation-->
  7. <label class="label">Username</label>
  8. <div class="control is-grouped">
  9. <p class="control is-expanded has-icon has-icon-right">
  10. <input
  11. v-model="user.username"
  12. class="input"
  13. type="text"
  14. placeholder="Change username"
  15. />
  16. <!--Remove validation if it's their own without changing-->
  17. </p>
  18. <p class="control">
  19. <button class="button is-success" @click="changeUsername()">
  20. Save changes
  21. </button>
  22. </p>
  23. </div>
  24. <label class="label">Email</label>
  25. <div v-if="user.email" class="control is-grouped">
  26. <p class="control is-expanded has-icon has-icon-right">
  27. <input
  28. v-model="user.email.address"
  29. class="input"
  30. type="text"
  31. placeholder="Change email address"
  32. />
  33. <!--Remove validation if it's their own without changing-->
  34. </p>
  35. <p class="control is-expanded">
  36. <button class="button is-success" @click="changeEmail()">
  37. Save changes
  38. </button>
  39. </p>
  40. </div>
  41. <label v-if="password" class="label">Change Password</label>
  42. <div v-if="password" class="control is-grouped">
  43. <p class="control is-expanded has-icon has-icon-right">
  44. <input
  45. v-model="newPassword"
  46. class="input"
  47. type="password"
  48. placeholder="Change password"
  49. />
  50. </p>
  51. <p class="control is-expanded">
  52. <button class="button is-success" @click="changePassword()">
  53. Change password
  54. </button>
  55. </p>
  56. </div>
  57. <label class="label">Location</label>
  58. <div v-if="user" class="control is-grouped">
  59. <p class="control is-expanded has-icon has-icon-right">
  60. <input
  61. v-model="user.location"
  62. class="input"
  63. type="text"
  64. placeholder="Change location"
  65. />
  66. </p>
  67. <p class="control is-expanded">
  68. <button class="button is-success" @click="changeLocation()">
  69. Save changes
  70. </button>
  71. </p>
  72. </div>
  73. <label class="label">Bio</label>
  74. <div v-if="user" class="control is-grouped">
  75. <p class="control is-expanded has-icon has-icon-right">
  76. <textarea
  77. v-model="user.bio"
  78. class="textarea"
  79. type="text"
  80. placeholder="Change bio"
  81. />
  82. </p>
  83. <p class="control is-expanded">
  84. <button class="button is-success" @click="changeBio()">
  85. Save changes
  86. </button>
  87. </p>
  88. </div>
  89. <label v-if="password" class="label">Use nightmode</label>
  90. <div v-if="password" class="control is-grouped">
  91. <input
  92. :checked="nightmode"
  93. type="checkbox"
  94. @click="toggleNightmode()"
  95. />
  96. </div>
  97. <label v-if="!password" class="label">Add password</label>
  98. <div v-if="!password" class="control is-grouped">
  99. <button
  100. v-if="passwordStep === 1"
  101. class="button is-success"
  102. @click="requestPassword()"
  103. >
  104. Request password email
  105. </button>
  106. <br />
  107. <p
  108. v-if="passwordStep === 2"
  109. class="control is-expanded has-icon has-icon-right"
  110. >
  111. <input
  112. v-model="passwordCode"
  113. class="input"
  114. type="text"
  115. placeholder="Code"
  116. />
  117. </p>
  118. <p v-if="passwordStep === 2" class="control is-expanded">
  119. <button class="button is-success" v-on:click="verifyCode()">
  120. Verify code
  121. </button>
  122. </p>
  123. <p
  124. v-if="passwordStep === 3"
  125. class="control is-expanded has-icon has-icon-right"
  126. >
  127. <input
  128. v-model="setNewPassword"
  129. class="input"
  130. type="password"
  131. placeholder="New password"
  132. />
  133. </p>
  134. <p v-if="passwordStep === 3" class="control is-expanded">
  135. <button class="button is-success" @click="setPassword()">
  136. Set password
  137. </button>
  138. </p>
  139. </div>
  140. <a
  141. v-if="passwordStep === 1 && !password"
  142. href="#"
  143. @click="passwordStep = 2"
  144. >Skip this step</a
  145. >
  146. <a
  147. v-if="!github"
  148. class="button is-github"
  149. :href="`${serverDomain}/auth/github/link`"
  150. >
  151. <div class="icon">
  152. <img class="invert" src="/assets/social/github.svg" />
  153. </div>
  154. &nbsp; Link GitHub to account
  155. </a>
  156. <button
  157. v-if="password && github"
  158. class="button is-danger"
  159. @click="unlinkPassword()"
  160. >
  161. Remove logging in with password
  162. </button>
  163. <button
  164. v-if="password && github"
  165. class="button is-danger"
  166. @click="unlinkGitHub()"
  167. >
  168. Remove logging in with GitHub
  169. </button>
  170. <br />
  171. <button
  172. class="button is-warning"
  173. style="margin-top: 30px;"
  174. @click="removeSessions()"
  175. >
  176. Log out everywhere
  177. </button>
  178. </div>
  179. <main-footer />
  180. </div>
  181. </template>
  182. <script>
  183. import { mapState, mapActions } from "vuex";
  184. import Toast from "toasters";
  185. import MainHeader from "../MainHeader.vue";
  186. import MainFooter from "../MainFooter.vue";
  187. import io from "../../io";
  188. import validation from "../../validation";
  189. export default {
  190. components: { MainHeader, MainFooter },
  191. data() {
  192. return {
  193. user: {},
  194. newPassword: "",
  195. password: false,
  196. github: false,
  197. setNewPassword: "",
  198. passwordStep: 1,
  199. passwordCode: "",
  200. serverDomain: ""
  201. };
  202. },
  203. computed: mapState({
  204. userId: state => state.user.auth.userId,
  205. nightmode: state => state.user.preferences.nightmode
  206. }),
  207. mounted() {
  208. lofig.get("serverDomain").then(serverDomain => {
  209. this.serverDomain = serverDomain;
  210. });
  211. io.getSocket(socket => {
  212. this.socket = socket;
  213. this.socket.emit("users.findBySession", res => {
  214. if (res.status === "success") {
  215. this.user = res.data;
  216. this.password = this.user.password;
  217. this.github = this.user.github;
  218. } else {
  219. new Toast({
  220. content: "Your are currently not signed in",
  221. timeout: 3000
  222. });
  223. }
  224. });
  225. this.socket.on("event:user.linkPassword", () => {
  226. this.password = true;
  227. });
  228. this.socket.on("event:user.linkGitHub", () => {
  229. this.github = true;
  230. });
  231. this.socket.on("event:user.unlinkPassword", () => {
  232. this.password = false;
  233. });
  234. this.socket.on("event:user.unlinkGitHub", () => {
  235. this.github = false;
  236. });
  237. });
  238. },
  239. methods: {
  240. changeEmail() {
  241. const email = this.user.email.address;
  242. if (!validation.isLength(email, 3, 254))
  243. return new Toast({
  244. content: "Email must have between 3 and 254 characters.",
  245. timeout: 8000
  246. });
  247. if (
  248. email.indexOf("@") !== email.lastIndexOf("@") ||
  249. !validation.regex.emailSimple.test(email)
  250. )
  251. return new Toast({
  252. content: "Invalid email format.",
  253. timeout: 8000
  254. });
  255. return this.socket.emit(
  256. "users.updateEmail",
  257. this.userId,
  258. email,
  259. res => {
  260. if (res.status !== "success")
  261. new Toast({ content: res.message, timeout: 8000 });
  262. else
  263. new Toast({
  264. content: "Successfully changed email address",
  265. timeout: 4000
  266. });
  267. }
  268. );
  269. },
  270. changeUsername() {
  271. const { username } = this.user;
  272. if (!validation.isLength(username, 2, 32))
  273. return new Toast({
  274. content: "Username must have between 2 and 32 characters.",
  275. timeout: 8000
  276. });
  277. if (!validation.regex.azAZ09_.test(username))
  278. return new Toast({
  279. content:
  280. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  281. timeout: 8000
  282. });
  283. return this.socket.emit(
  284. "users.updateUsername",
  285. this.userId,
  286. username,
  287. res => {
  288. if (res.status !== "success")
  289. new Toast({ content: res.message, timeout: 8000 });
  290. else
  291. new Toast({
  292. content: "Successfully changed username",
  293. timeout: 4000
  294. });
  295. }
  296. );
  297. },
  298. changeLocation() {
  299. const { location } = this.user;
  300. if (!validation.isLength(location, 0, 50))
  301. return new Toast({
  302. content: "Location must have between 0 and 50 characters.",
  303. timeout: 8000
  304. });
  305. return this.socket.emit(
  306. "users.updateLocation",
  307. this.userId,
  308. location,
  309. res => {
  310. if (res.status !== "success")
  311. new Toast({ content: res.message, timeout: 8000 });
  312. else
  313. new Toast({
  314. content: "Successfully changed location",
  315. timeout: 4000
  316. });
  317. }
  318. );
  319. },
  320. changeBio() {
  321. const { bio } = this.user;
  322. if (!validation.isLength(bio, 0, 200))
  323. return new Toast({
  324. content: "Bio must have between 0 and 200 characters.",
  325. timeout: 8000
  326. });
  327. return this.socket.emit(
  328. "users.updateBio",
  329. this.userId,
  330. bio,
  331. res => {
  332. if (res.status !== "success")
  333. new Toast({ content: res.message, timeout: 8000 });
  334. else
  335. new Toast({
  336. content: "Successfully changed bio",
  337. timeout: 4000
  338. });
  339. }
  340. );
  341. },
  342. changePassword() {
  343. const { newPassword } = this;
  344. if (!validation.isLength(newPassword, 6, 200))
  345. return new Toast({
  346. content: "Password must have between 6 and 200 characters.",
  347. timeout: 8000
  348. });
  349. if (!validation.regex.password.test(newPassword))
  350. return new Toast({
  351. content:
  352. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  353. timeout: 8000
  354. });
  355. return this.socket.emit(
  356. "users.updatePassword",
  357. newPassword,
  358. res => {
  359. if (res.status !== "success")
  360. new Toast({ content: res.message, timeout: 8000 });
  361. else
  362. new Toast({
  363. content: "Successfully changed password",
  364. timeout: 4000
  365. });
  366. }
  367. );
  368. },
  369. requestPassword() {
  370. return this.socket.emit("users.requestPassword", res => {
  371. new Toast({ content: res.message, timeout: 8000 });
  372. if (res.status === "success") {
  373. this.passwordStep = 2;
  374. }
  375. });
  376. },
  377. verifyCode() {
  378. if (!this.passwordCode)
  379. return new Toast({
  380. content: "Code cannot be empty",
  381. timeout: 8000
  382. });
  383. return this.socket.emit(
  384. "users.verifyPasswordCode",
  385. this.passwordCode,
  386. res => {
  387. new Toast({ content: res.message, timeout: 8000 });
  388. if (res.status === "success") {
  389. this.passwordStep = 3;
  390. }
  391. }
  392. );
  393. },
  394. setPassword() {
  395. const newPassword = this.setNewPassword;
  396. if (!validation.isLength(newPassword, 6, 200))
  397. return new Toast({
  398. content: "Password must have between 6 and 200 characters.",
  399. timeout: 8000
  400. });
  401. if (!validation.regex.password.test(newPassword))
  402. return new Toast({
  403. content:
  404. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  405. timeout: 8000
  406. });
  407. return this.socket.emit(
  408. "users.changePasswordWithCode",
  409. this.passwordCode,
  410. newPassword,
  411. res => {
  412. new Toast({ content: res.message, timeout: 8000 });
  413. }
  414. );
  415. },
  416. unlinkPassword() {
  417. this.socket.emit("users.unlinkPassword", res => {
  418. new Toast({ content: res.message, timeout: 8000 });
  419. });
  420. },
  421. unlinkGitHub() {
  422. this.socket.emit("users.unlinkGitHub", res => {
  423. new Toast({ content: res.message, timeout: 8000 });
  424. });
  425. },
  426. removeSessions() {
  427. this.socket.emit(`users.removeSessions`, this.userId, res => {
  428. new Toast({ content: res.message, timeout: 4000 });
  429. });
  430. },
  431. toggleNightmode() {
  432. localStorage.setItem("nightmode", !this.nightmode);
  433. this.changeNightmode(!this.nightmode);
  434. },
  435. ...mapActions("user/preferences", ["changeNightmode"])
  436. }
  437. };
  438. </script>
  439. <style lang="scss" scoped>
  440. @import "styles/global.scss";
  441. .night-mode {
  442. .label {
  443. color: #ddd;
  444. }
  445. }
  446. .container {
  447. padding: 25px;
  448. }
  449. a {
  450. color: $primary-color !important;
  451. }
  452. </style>