web/turnstile: refresh turnstile if it expires in background
also renamed `turnstileLoaded` to `turnstileSolved` for more clarity
This commit is contained in:
@@ -5,7 +5,7 @@ import lazySettingGetter from "$lib/settings/lazy-get";
|
||||
|
||||
import { getSession } from "$lib/api/session";
|
||||
import { currentApiURL } from "$lib/api/api-url";
|
||||
import { turnstileLoaded } from "$lib/state/turnstile";
|
||||
import { turnstileSolved } from "$lib/state/turnstile";
|
||||
import { cachedInfo, getServerInfo } from "$lib/api/server-info";
|
||||
|
||||
import type { Optional } from "$lib/types/generic";
|
||||
@@ -49,7 +49,7 @@ const request = async (url: string) => {
|
||||
} as CobaltErrorResponse;
|
||||
}
|
||||
|
||||
if (getCachedInfo?.info?.cobalt?.turnstileSitekey && !get(turnstileLoaded)) {
|
||||
if (getCachedInfo?.info?.cobalt?.turnstileSitekey && !get(turnstileSolved)) {
|
||||
return {
|
||||
status: "error",
|
||||
error: {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { turnstileSolved } from "$lib/state/turnstile";
|
||||
|
||||
const getResponse = () => {
|
||||
const turnstileElement = document.getElementById("turnstile-widget");
|
||||
|
||||
@@ -12,13 +14,28 @@ const update = () => {
|
||||
const turnstileElement = document.getElementById("turnstile-widget");
|
||||
|
||||
if (turnstileElement) {
|
||||
turnstileSolved.set(false);
|
||||
return window?.turnstile?.reset(turnstileElement);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const refreshIfExpired = () => {
|
||||
const turnstileElement = document.getElementById("turnstile-widget");
|
||||
|
||||
if (turnstileElement) {
|
||||
const isExpired = window?.turnstile?.isExpired(turnstileElement);
|
||||
if (isExpired) {
|
||||
console.log("expired, refreshing the turnstile widget rn");
|
||||
return update();
|
||||
}
|
||||
console.log("turnstile not expired, nothing to do");
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
getResponse,
|
||||
update,
|
||||
refreshIfExpired,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const turnstileLoaded = writable(false);
|
||||
export const turnstileSolved = writable(false);
|
||||
export const turnstileCreated = writable(false);
|
||||
|
||||
Reference in New Issue
Block a user