auth.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import io from "../io";
  2. // when Vuex needs to interact with socket.io
  3. export default {
  4. register(user) {
  5. return new Promise((resolve, reject) => {
  6. const { username, email, password, recaptchaToken } = user;
  7. io.getSocket(socket => {
  8. socket.emit(
  9. "users.register",
  10. username,
  11. email,
  12. password,
  13. recaptchaToken,
  14. res => {
  15. if (res.status === "success") {
  16. if (res.SID) {
  17. return lofig.get("cookie", cookie => {
  18. const date = new Date();
  19. date.setTime(
  20. new Date().getTime() +
  21. 2 * 365 * 24 * 60 * 60 * 1000
  22. );
  23. const secure = cookie.secure
  24. ? "secure=true; "
  25. : "";
  26. document.cookie = `SID=${
  27. res.SID
  28. }; expires=${date.toGMTString()}; domain=${
  29. cookie.domain
  30. }; ${secure}path=/`;
  31. return resolve({ status: "success" });
  32. });
  33. }
  34. return reject(new Error("You must login"));
  35. }
  36. return reject(new Error(res.message));
  37. }
  38. );
  39. });
  40. });
  41. },
  42. login(user) {
  43. return new Promise((resolve, reject) => {
  44. const { email, password } = user;
  45. io.getSocket(socket => {
  46. socket.emit("users.login", email, password, res => {
  47. if (res.status === "success") {
  48. return lofig.get("cookie", cookie => {
  49. const date = new Date();
  50. date.setTime(
  51. new Date().getTime() +
  52. 2 * 365 * 24 * 60 * 60 * 1000
  53. );
  54. const secure = cookie.secure ? "secure=true; " : "";
  55. let domain = "";
  56. if (cookie.domain !== "localhost")
  57. domain = ` domain=${cookie.domain};`;
  58. document.cookie = `SID=${
  59. res.SID
  60. }; expires=${date.toGMTString()}; ${domain}${secure}path=/`;
  61. return resolve({ status: "success" });
  62. });
  63. }
  64. return reject(new Error(res.message));
  65. });
  66. });
  67. });
  68. }
  69. };