Settings.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 v-if="password" class="label">Use nightmode</label>
  58. <div v-if="password" class="control is-grouped">
  59. <input
  60. :checked="nightmode"
  61. type="checkbox"
  62. @click="toggleNightmode()"
  63. />
  64. </div>
  65. <label v-if="!password" class="label">Add password</label>
  66. <div v-if="!password" class="control is-grouped">
  67. <button
  68. v-if="passwordStep === 1"
  69. class="button is-success"
  70. @click="requestPassword()"
  71. >
  72. Request password email
  73. </button>
  74. <br />
  75. <p
  76. v-if="passwordStep === 2"
  77. class="control is-expanded has-icon has-icon-right"
  78. >
  79. <input
  80. v-model="passwordCode"
  81. class="input"
  82. type="text"
  83. placeholder="Code"
  84. />
  85. </p>
  86. <p v-if="passwordStep === 2" class="control is-expanded">
  87. <button class="button is-success" v-on:click="verifyCode()">
  88. Verify code
  89. </button>
  90. </p>
  91. <p
  92. v-if="passwordStep === 3"
  93. class="control is-expanded has-icon has-icon-right"
  94. >
  95. <input
  96. v-model="setNewPassword"
  97. class="input"
  98. type="password"
  99. placeholder="New password"
  100. />
  101. </p>
  102. <p v-if="passwordStep === 3" class="control is-expanded">
  103. <button class="button is-success" @click="setPassword()">
  104. Set password
  105. </button>
  106. </p>
  107. </div>
  108. <a
  109. v-if="passwordStep === 1 && !password"
  110. href="#"
  111. @click="passwordStep = 2"
  112. >Skip this step</a
  113. >
  114. <a
  115. v-if="!github"
  116. class="button is-github"
  117. :href="`${serverDomain}/auth/github/link`"
  118. >
  119. <div class="icon">
  120. <img class="invert" src="/assets/social/github.svg" />
  121. </div>
  122. &nbsp; Link GitHub to account
  123. </a>
  124. <button
  125. v-if="password && github"
  126. class="button is-danger"
  127. @click="unlinkPassword()"
  128. >
  129. Remove logging in with password
  130. </button>
  131. <button
  132. v-if="password && github"
  133. class="button is-danger"
  134. @click="unlinkGitHub()"
  135. >
  136. Remove logging in with GitHub
  137. </button>
  138. <br />
  139. <button
  140. class="button is-warning"
  141. style="margin-top: 30px;"
  142. @click="removeSessions()"
  143. >
  144. Log out everywhere
  145. </button>
  146. </div>
  147. <main-footer />
  148. </div>
  149. </template>
  150. <script>
  151. import { mapState, mapActions } from "vuex";
  152. import Toast from "toasters";
  153. import MainHeader from "../MainHeader.vue";
  154. import MainFooter from "../MainFooter.vue";
  155. import io from "../../io";
  156. import validation from "../../validation";
  157. export default {
  158. components: { MainHeader, MainFooter },
  159. data() {
  160. return {
  161. user: {},
  162. newPassword: "",
  163. password: false,
  164. github: false,
  165. setNewPassword: "",
  166. passwordStep: 1,
  167. passwordCode: "",
  168. serverDomain: ""
  169. };
  170. },
  171. computed: mapState({
  172. userId: state => state.user.auth.userId,
  173. nightmode: state => state.user.preferences.nightmode
  174. }),
  175. mounted() {
  176. lofig.get("serverDomain").then(serverDomain => {
  177. this.serverDomain = serverDomain;
  178. });
  179. io.getSocket(socket => {
  180. this.socket = socket;
  181. this.socket.emit("users.findBySession", res => {
  182. if (res.status === "success") {
  183. this.user = res.data;
  184. this.password = this.user.password;
  185. this.github = this.user.github;
  186. } else {
  187. new Toast({
  188. content: "Your are currently not signed in",
  189. timeout: 3000
  190. });
  191. }
  192. });
  193. this.socket.on("event:user.linkPassword", () => {
  194. this.password = true;
  195. });
  196. this.socket.on("event:user.linkGitHub", () => {
  197. this.github = true;
  198. });
  199. this.socket.on("event:user.unlinkPassword", () => {
  200. this.password = false;
  201. });
  202. this.socket.on("event:user.unlinkGitHub", () => {
  203. this.github = false;
  204. });
  205. });
  206. },
  207. methods: {
  208. changeEmail() {
  209. const email = this.user.email.address;
  210. if (!validation.isLength(email, 3, 254))
  211. return new Toast({
  212. content: "Email must have between 3 and 254 characters.",
  213. timeout: 8000
  214. });
  215. if (
  216. email.indexOf("@") !== email.lastIndexOf("@") ||
  217. !validation.regex.emailSimple.test(email)
  218. )
  219. return new Toast({
  220. content: "Invalid email format.",
  221. timeout: 8000
  222. });
  223. return this.socket.emit(
  224. "users.updateEmail",
  225. this.userId,
  226. email,
  227. res => {
  228. if (res.status !== "success")
  229. new Toast({ content: res.message, timeout: 8000 });
  230. else
  231. new Toast({
  232. content: "Successfully changed email address",
  233. timeout: 4000
  234. });
  235. }
  236. );
  237. },
  238. changeUsername() {
  239. const { username } = this.user;
  240. if (!validation.isLength(username, 2, 32))
  241. return new Toast({
  242. content: "Username must have between 2 and 32 characters.",
  243. timeout: 8000
  244. });
  245. if (!validation.regex.azAZ09_.test(username))
  246. return new Toast({
  247. content:
  248. "Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.",
  249. timeout: 8000
  250. });
  251. return this.socket.emit(
  252. "users.updateUsername",
  253. this.userId,
  254. username,
  255. res => {
  256. if (res.status !== "success")
  257. new Toast({ content: res.message, timeout: 8000 });
  258. else
  259. new Toast({
  260. content: "Successfully changed username",
  261. timeout: 4000
  262. });
  263. }
  264. );
  265. },
  266. changePassword() {
  267. const { newPassword } = this;
  268. if (!validation.isLength(newPassword, 6, 200))
  269. return new Toast({
  270. content: "Password must have between 6 and 200 characters.",
  271. timeout: 8000
  272. });
  273. if (!validation.regex.password.test(newPassword))
  274. return new Toast({
  275. content:
  276. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  277. timeout: 8000
  278. });
  279. return this.socket.emit(
  280. "users.updatePassword",
  281. newPassword,
  282. res => {
  283. if (res.status !== "success")
  284. new Toast({ content: res.message, timeout: 8000 });
  285. else
  286. new Toast({
  287. content: "Successfully changed password",
  288. timeout: 4000
  289. });
  290. }
  291. );
  292. },
  293. requestPassword() {
  294. return this.socket.emit("users.requestPassword", res => {
  295. new Toast({ content: res.message, timeout: 8000 });
  296. if (res.status === "success") {
  297. this.passwordStep = 2;
  298. }
  299. });
  300. },
  301. verifyCode() {
  302. if (!this.passwordCode)
  303. return new Toast({
  304. content: "Code cannot be empty",
  305. timeout: 8000
  306. });
  307. return this.socket.emit(
  308. "users.verifyPasswordCode",
  309. this.passwordCode,
  310. res => {
  311. new Toast({ content: res.message, timeout: 8000 });
  312. if (res.status === "success") {
  313. this.passwordStep = 3;
  314. }
  315. }
  316. );
  317. },
  318. setPassword() {
  319. const newPassword = this.setNewPassword;
  320. if (!validation.isLength(newPassword, 6, 200))
  321. return new Toast({
  322. content: "Password must have between 6 and 200 characters.",
  323. timeout: 8000
  324. });
  325. if (!validation.regex.password.test(newPassword))
  326. return new Toast({
  327. content:
  328. "Invalid password format. Must have one lowercase letter, one uppercase letter, one number and one special character.",
  329. timeout: 8000
  330. });
  331. return this.socket.emit(
  332. "users.changePasswordWithCode",
  333. this.passwordCode,
  334. newPassword,
  335. res => {
  336. new Toast({ content: res.message, timeout: 8000 });
  337. }
  338. );
  339. },
  340. unlinkPassword() {
  341. this.socket.emit("users.unlinkPassword", res => {
  342. new Toast({ content: res.message, timeout: 8000 });
  343. });
  344. },
  345. unlinkGitHub() {
  346. this.socket.emit("users.unlinkGitHub", res => {
  347. new Toast({ content: res.message, timeout: 8000 });
  348. });
  349. },
  350. removeSessions() {
  351. this.socket.emit(`users.removeSessions`, this.userId, res => {
  352. new Toast({ content: res.message, timeout: 4000 });
  353. });
  354. },
  355. toggleNightmode() {
  356. localStorage.setItem("nightmode", !this.nightmode);
  357. this.changeNightmode(!this.nightmode);
  358. },
  359. ...mapActions("user/preferences", ["changeNightmode"])
  360. }
  361. };
  362. </script>
  363. <style lang="scss" scoped>
  364. @import "styles/global.scss";
  365. .night-mode {
  366. .label {
  367. color: #ddd;
  368. }
  369. }
  370. .container {
  371. padding: 25px;
  372. }
  373. a {
  374. color: $primary-color !important;
  375. }
  376. </style>