UNPKG

fastapi-rtk

Version:

A React component library for FastAPI in combination with FastAPI React Toolkit backend, built with Mantine, JsonForms, and Zustand.

91 lines (90 loc) 3.22 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const utils = require("fastapi-rtk/utils"); async function authSignin(baseUrl, { username, password }) { const { fetchPath, options } = utils.createFetchParams({ path: utils.urlJoin(baseUrl, "auth/login"), method: "POST", body: { username, password } }); const res = await fetch(fetchPath, options); if (!res.ok) { if (res.status === 400) { throw new Error("Username or password wrong."); } else { throw new Error("Failed to sign in."); } } } async function authSignout(baseUrl) { const { fetchPath, options } = utils.createFetchParams({ path: utils.urlJoin(baseUrl, "auth/logout"), method: "GET" }); const res = await fetch(fetchPath, options); if (!res.ok) { throw new Error("Failed to sign out."); } } async function authResetPassword(baseUrl, newPassword, { email }) { const { fetchPath, options } = utils.createFetchParams({ path: utils.urlJoin(baseUrl, "auth/forgot-password"), method: "POST", body: { email } }); const forgotPasswordRes = await fetch(fetchPath, options); if (!forgotPasswordRes.ok) { throw new Error("Could not request token to reset password"); } const jsonResponse = await forgotPasswordRes.json(); const { token } = jsonResponse; const { fetchPath: fetchPath2, options: options2 } = utils.createFetchParams({ path: utils.urlJoin(baseUrl, "auth/reset-password"), method: "POST", body: { token, password: newPassword } }); const resetPasswordRes = await fetch(fetchPath2, options2); if (!resetPasswordRes.ok) { throw new Error("Could not reset password"); } } function authOAuthSignin(baseUrl, provider, popup, afterSigninPopup) { const url = utils.urlJoin(baseUrl, "auth/login", provider); if (!popup) { window.location.href = utils.urlJoin(baseUrl, "auth/login", provider); return; } const width = 500; const height = 500; const left = window.screenLeft + window.outerWidth / 2 - width / 2; const top = window.screenTop + window.outerHeight / 2 - height / 2; const authWindow = window.open( url + "?popup=true", "auth", `toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${top}, left=${left}` ); const pollTimer = window.setInterval(function() { if (authWindow.closed !== false) { window.clearInterval(pollTimer); afterSigninPopup == null ? void 0 : afterSigninPopup(); } }, 200); } async function authInfo(baseUrl, signal) { const { fetchPath, options } = utils.createFetchParams({ path: utils.urlJoin(baseUrl, "info/"), method: "GET" }); const res = await fetch(fetchPath, { signal, ...options }); const data = await res.json(); if (!res.ok) { const errorMessage = typeof data.message === "string" ? data.message : JSON.stringify(data.message, null, 2); throw new Error(errorMessage); } return data; } exports.authInfo = authInfo; exports.authOAuthSignin = authOAuthSignin; exports.authResetPassword = authResetPassword; exports.authSignin = authSignin; exports.authSignout = authSignout;