fix: refresh login fallback in mini app
This commit is contained in:
@@ -64,11 +64,16 @@
|
||||
container.appendChild(script);
|
||||
};
|
||||
|
||||
window.watchTelegramMiniAppLogin = function (statusUrl, redirectUrl) {
|
||||
window.watchTelegramMiniAppLogin = function (statusUrl, redirectUrl, reloadOnReturn) {
|
||||
window.gmRelayMiniAppLoginReloadOnReturn =
|
||||
window.gmRelayMiniAppLoginReloadOnReturn || reloadOnReturn === true;
|
||||
|
||||
if (window.gmRelayMiniAppLoginWatcher) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.gmRelayMiniAppLoginLeftPage = false;
|
||||
|
||||
var stopWatching = function () {
|
||||
if (window.gmRelayMiniAppLoginWatcher) {
|
||||
window.clearInterval(window.gmRelayMiniAppLoginWatcher);
|
||||
@@ -76,7 +81,41 @@
|
||||
}
|
||||
};
|
||||
|
||||
var checkLogin = async function () {
|
||||
var reloadAfterExternalLogin = function () {
|
||||
if (!window.gmRelayMiniAppLoginReloadOnReturn || !window.gmRelayMiniAppLoginLeftPage) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.gmRelayMiniAppLoginLeftPage = false;
|
||||
|
||||
try {
|
||||
var refreshKey = 'gmrelay-miniapp-login-refresh:' + window.location.pathname;
|
||||
if (window.sessionStorage && window.sessionStorage.getItem(refreshKey) === '1') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.sessionStorage) {
|
||||
window.sessionStorage.setItem(refreshKey, '1');
|
||||
}
|
||||
} catch (error) {
|
||||
}
|
||||
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
var allowNextExternalLoginReload = function () {
|
||||
window.gmRelayMiniAppLoginLeftPage = true;
|
||||
|
||||
try {
|
||||
var refreshKey = 'gmrelay-miniapp-login-refresh:' + window.location.pathname;
|
||||
if (window.sessionStorage) {
|
||||
window.sessionStorage.removeItem(refreshKey);
|
||||
}
|
||||
} catch (error) {
|
||||
}
|
||||
};
|
||||
|
||||
var checkLogin = async function (reloadWhenUnauthenticated) {
|
||||
try {
|
||||
var response = await fetch(statusUrl, {
|
||||
credentials: 'same-origin',
|
||||
@@ -91,6 +130,11 @@
|
||||
if (payload.authenticated) {
|
||||
stopWatching();
|
||||
window.location.href = redirectUrl || '/';
|
||||
return;
|
||||
}
|
||||
|
||||
if (reloadWhenUnauthenticated) {
|
||||
reloadAfterExternalLogin();
|
||||
}
|
||||
} catch (error) {
|
||||
return;
|
||||
@@ -98,16 +142,22 @@
|
||||
};
|
||||
|
||||
window.gmRelayMiniAppLoginWatcher = window.setInterval(checkLogin, 1000);
|
||||
window.addEventListener('blur', function () {
|
||||
allowNextExternalLoginReload();
|
||||
});
|
||||
window.addEventListener('focus', function () {
|
||||
void checkLogin();
|
||||
void checkLogin(true);
|
||||
});
|
||||
document.addEventListener('visibilitychange', function () {
|
||||
if (!document.hidden) {
|
||||
void checkLogin();
|
||||
if (document.hidden) {
|
||||
allowNextExternalLoginReload();
|
||||
return;
|
||||
}
|
||||
|
||||
void checkLogin(true);
|
||||
});
|
||||
|
||||
void checkLogin();
|
||||
void checkLogin(false);
|
||||
};
|
||||
|
||||
window.authenticateTelegramMiniApp = async function (authUrl, redirectUrl) {
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="nav-version">v1.9.1</div>
|
||||
<div class="nav-version">v1.9.2</div>
|
||||
</div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
if (firstRender)
|
||||
{
|
||||
await JS.InvokeVoidAsync("loadTelegramWidget", BotUsername, AuthUrl);
|
||||
await JS.InvokeVoidAsync("watchTelegramMiniAppLogin", "/auth/status", "/", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user