Comparing version
@@ -38,2 +38,3 @@ import { createClient } from "@supabase/supabase-js"; | ||
}>; | ||
clearSessionStorage(): void; | ||
signInWithPassword(credentials: { | ||
@@ -40,0 +41,0 @@ email: string; |
@@ -120,3 +120,3 @@ import axios, { AxiosError } from "axios"; | ||
if (!token) { | ||
window.localStorage.removeItem("polyfact_refresh_token"); | ||
this.clearSessionStorage(); | ||
return {}; | ||
@@ -131,2 +131,5 @@ } | ||
} | ||
clearSessionStorage() { | ||
window.localStorage.removeItem("polyfact_refresh_token"); | ||
} | ||
signInWithPassword(credentials) { | ||
@@ -133,0 +136,0 @@ this.isAuthTokenSetSync = true; |
import { Client } from "../client"; | ||
type Provider = "github" | "google"; | ||
type SimpleProvider = "github" | "google"; | ||
type LoginWithFirebaseInput = { | ||
token: string; | ||
provider: "firebase"; | ||
}; | ||
type LoginFunctionInput = SimpleProvider | { | ||
provider: SimpleProvider; | ||
} | LoginWithFirebaseInput; | ||
export default function usePolyfact(args: { | ||
project: string; | ||
endpoint?: string; | ||
} | null): { | ||
} | string | null): { | ||
polyfact: Client | undefined; | ||
login: ((input: { | ||
provider: Provider; | ||
}) => Promise<void>) | undefined; | ||
loginWithFirebase: ((token: string) => Promise<void>) | undefined; | ||
login: ((input: LoginFunctionInput) => Promise<void>) | undefined; | ||
logout: () => Promise<void>; | ||
email?: string; | ||
@@ -13,0 +18,0 @@ loading: boolean; |
@@ -5,10 +5,36 @@ import { Mutex } from "async-mutex"; | ||
const reactMutex = new Mutex(); | ||
function loginFunctionBuilder({ project, endpoint }) { | ||
return async function loginFunction(input) { | ||
if (typeof input === "object" && input.provider === "firebase") { | ||
return await Polyfact.endpoint(endpoint || "https://api.polyfact.com") | ||
.project(project) | ||
.signInWithFirebaseToken(input.token); | ||
} | ||
let provider; | ||
if (typeof input === "string") { | ||
provider = input; | ||
} | ||
else { | ||
provider = input.provider; | ||
} | ||
await Polyfact.endpoint(endpoint || "https://api.polyfact.com") | ||
.project(project) | ||
.oAuthRedirect({ provider }); | ||
return new Promise(() => { }); | ||
}; | ||
} | ||
export default function usePolyfact(args) { | ||
const { project, endpoint } = args || {}; | ||
let project; | ||
let endpoint; | ||
if (typeof args === "string") { | ||
project = args; | ||
} | ||
else if (typeof args === "object") { | ||
({ project, endpoint } = args || {}); | ||
} | ||
const [polyfact, setPolyfact] = useState(); | ||
const [email, setEmail] = useState(); | ||
const [loading, setLoading] = useState(true); | ||
const [login, setLogin] = useState(); | ||
const [loginWithFirebase, setLoginWithFirebase] = useState(); | ||
const [polyfactPromise] = useState(() => { | ||
const [allowLogin, setAllowLogin] = useState(false); | ||
function initPolyfactPromise() { | ||
let resolveFunction; | ||
@@ -19,3 +45,25 @@ const promise = new Promise((resolve) => { | ||
return { promise, resolveFunction: resolveFunction }; | ||
}); | ||
} | ||
const [polyfactPromise, setPolyfactPromise] = useState(() => initPolyfactPromise()); | ||
async function loginFunction(input) { | ||
if (project) { | ||
const p = await loginFunctionBuilder({ project, endpoint })(input); | ||
setAllowLogin(false); | ||
setPolyfact(p); | ||
polyfactPromise.resolveFunction(p); | ||
window.Polyfact = p; | ||
} | ||
} | ||
async function logout() { | ||
if (!loading && polyfact) { | ||
setPolyfact(undefined); | ||
setAllowLogin(true); | ||
setEmail(undefined); | ||
setPolyfactPromise(() => initPolyfactPromise()); | ||
setLoading(false); | ||
} | ||
else { | ||
throw new Error("Polyfact is not initialized"); | ||
} | ||
} | ||
useEffect(() => { | ||
@@ -46,17 +94,3 @@ if (project) { | ||
else { | ||
setLogin(() => async ({ provider }) => { | ||
await Polyfact.endpoint(endpoint || "https://api.polyfact.com") | ||
.project(project) | ||
.oAuthRedirect({ provider }); | ||
}); | ||
setLoginWithFirebase(() => async (token) => { | ||
const p = await Polyfact.endpoint(endpoint || "https://api.polyfact.com") | ||
.project(project) | ||
.signInWithFirebaseToken(token); | ||
setLogin(undefined); | ||
setLoginWithFirebase(undefined); | ||
setPolyfact(p); | ||
polyfactPromise.resolveFunction(p); | ||
window.Polyfact = p; | ||
}); | ||
setAllowLogin(true); | ||
setLoading(false); | ||
@@ -87,8 +121,8 @@ } | ||
polyfact, | ||
login, | ||
login: allowLogin ? loginFunction : undefined, | ||
logout, | ||
loading, | ||
email, | ||
loginWithFirebase, | ||
polyfactPromise: polyfactPromise.promise, | ||
}; | ||
} |
{ | ||
"name": "polyfact", | ||
"version": "0.1.69", | ||
"version": "0.1.70", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2358888
0.04%2011
2.18%0
-100%