Settings.vue 8.8 KB

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