Files
console-dev/app/lib/useOAuth.js
T
2026-11-06 00:04:38 +08:00

43 lines
1.0 KiB
JavaScript

import { onBeforeUnmount } from "vue";
import { ref } from "vue";
import { useApi } from "~/lib/auth.js";
import { r as oauth_r } from "~/lib/oauth.js";
function i() {
let i = useApi(),
a = ref(!1),
o = ref(``),
s = !0;
onBeforeUnmount(() => {
s = !1;
});
async function c(e) {
if (s && !a.value) {
if (((o.value = ``), !/^[a-z][a-z0-9_]*$/i.test(e))) {
o.value = `授权方式无效`;
return;
}
a.value = !0;
try {
let t = await i.get(`/oauth/${encodeURIComponent(e)}`);
if (!s) return;
let n = oauth_r(t?.url, window.location.origin);
if (!n) throw Error(`授权地址无效,请联系支持`);
try {
sessionStorage.setItem(`console-oauth-pending`, `1`);
} catch {}
window.location.assign(n);
} catch (e) {
s && (o.value = e instanceof Error ? e.message : `授权失败`);
} finally {
s && (a.value = !1);
}
}
}
return {
busy: a,
error: o,
start: c,
};
}
export { i as t };