index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. };