index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getSessionUser = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. }
  10. })
  11. .then(async (res) => {
  12. if (!res.ok) throw await res.json();
  13. return res.json();
  14. })
  15. .catch((err) => {
  16. console.log(err);
  17. error = err.detail;
  18. return null;
  19. });
  20. if (error) {
  21. throw error;
  22. }
  23. return res;
  24. };
  25. export const userSignIn = async (email: string, password: string) => {
  26. let error = null;
  27. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signin`, {
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/json'
  31. },
  32. body: JSON.stringify({
  33. email: email,
  34. password: password
  35. })
  36. })
  37. .then(async (res) => {
  38. if (!res.ok) throw await res.json();
  39. return res.json();
  40. })
  41. .catch((err) => {
  42. console.log(err);
  43. error = err.detail;
  44. return null;
  45. });
  46. if (error) {
  47. throw error;
  48. }
  49. return res;
  50. };
  51. export const userSignUp = async (name: string, email: string, password: string) => {
  52. let error = null;
  53. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup`, {
  54. method: 'POST',
  55. headers: {
  56. 'Content-Type': 'application/json'
  57. },
  58. body: JSON.stringify({
  59. name: name,
  60. email: email,
  61. password: password
  62. })
  63. })
  64. .then(async (res) => {
  65. if (!res.ok) throw await res.json();
  66. return res.json();
  67. })
  68. .catch((err) => {
  69. console.log(err);
  70. error = err.detail;
  71. return null;
  72. });
  73. if (error) {
  74. throw error;
  75. }
  76. return res;
  77. };
  78. export const updateUserPassword = async (token: string, password: string, newPassword: string) => {
  79. let error = null;
  80. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/update/password`, {
  81. method: 'POST',
  82. headers: {
  83. 'Content-Type': 'application/json',
  84. ...(token && { authorization: `Bearer ${token}` })
  85. },
  86. body: JSON.stringify({
  87. password: password,
  88. new_password: newPassword
  89. })
  90. })
  91. .then(async (res) => {
  92. if (!res.ok) throw await res.json();
  93. return res.json();
  94. })
  95. .catch((err) => {
  96. console.log(err);
  97. error = err.detail;
  98. return null;
  99. });
  100. if (error) {
  101. throw error;
  102. }
  103. return res;
  104. };
  105. export const getSignUpEnabledStatus = async (token: string) => {
  106. let error = null;
  107. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled`, {
  108. method: 'GET',
  109. headers: {
  110. 'Content-Type': 'application/json',
  111. Authorization: `Bearer ${token}`
  112. }
  113. })
  114. .then(async (res) => {
  115. if (!res.ok) throw await res.json();
  116. return res.json();
  117. })
  118. .catch((err) => {
  119. console.log(err);
  120. error = err.detail;
  121. return null;
  122. });
  123. if (error) {
  124. throw error;
  125. }
  126. return res;
  127. };
  128. export const toggleSignUpEnabledStatus = async (token: string) => {
  129. let error = null;
  130. const res = await fetch(`${WEBUI_API_BASE_URL}/auths/signup/enabled/toggle`, {
  131. method: 'GET',
  132. headers: {
  133. 'Content-Type': 'application/json',
  134. Authorization: `Bearer ${token}`
  135. }
  136. })
  137. .then(async (res) => {
  138. if (!res.ok) throw await res.json();
  139. return res.json();
  140. })
  141. .catch((err) => {
  142. console.log(err);
  143. error = err.detail;
  144. return null;
  145. });
  146. if (error) {
  147. throw error;
  148. }
  149. return res;
  150. };