fix: registration and registration detection handling
This commit is contained in:
parent
febdbb9421
commit
038b1af44e
3 changed files with 34 additions and 25 deletions
|
@ -51,29 +51,17 @@
|
|||
|
||||
<script lang="ts" setup>
|
||||
import { FetchError } from 'ofetch';
|
||||
import type { StatsResponse } from '~/types/interfaces';
|
||||
|
||||
const instanceUrl = ref<string | null | undefined>(null);
|
||||
const instanceUrlInput = ref<string>();
|
||||
const instanceError = ref<string>();
|
||||
const requestUrl = useRequestURL();
|
||||
const apiVersion = useRuntimeConfig().public.apiVersion;
|
||||
const apiBase = useCookie("api_base");
|
||||
const gorbTxtError = ref<string>("");
|
||||
const registrationEnabled = useState("registrationEnabled", () => true);
|
||||
|
||||
const auth = useAuth();
|
||||
|
||||
const { status, data: gorbTxt } = await useFetch(`${requestUrl.protocol}//${requestUrl.host}/.well-known/gorb.txt`, { responseType: "text" });
|
||||
if (status.value == "success" && gorbTxt.value) {
|
||||
console.log("got gorb.txt:", gorbTxt.value);
|
||||
const parsed = parseWellKnown(gorbTxt.value as string);
|
||||
if (parsed.ApiBaseUrl) {
|
||||
apiBase.value = `${parsed.ApiBaseUrl}/${apiVersion}`;
|
||||
console.log("set apiBase to:", parsed.ApiBaseUrl);
|
||||
}
|
||||
} else {
|
||||
gorbTxtError.value = "Failed to find that instance.";
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
const cookie = useCookie("instance_url").value;
|
||||
instanceUrl.value = cookie;
|
||||
|
@ -99,6 +87,11 @@ async function selectInstance(e: Event) {
|
|||
instanceUrl.value = origin;
|
||||
useCookie("instance_url").value = origin;
|
||||
console.log("set instance url to:", origin);
|
||||
const { status, data, error } = await useFetch<StatsResponse>(`${apiBase.value}/stats`);
|
||||
if (status.value == "success" && data.value) {
|
||||
registrationEnabled.value = data.value.registration_enabled;
|
||||
console.log("set registration enabled value to:", data.value.registration_enabled);
|
||||
}
|
||||
return;
|
||||
}
|
||||
instanceError.value = "That URL is not a valid instance.";
|
||||
|
|
|
@ -3,6 +3,25 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
|||
const loading = useState("loading");
|
||||
const accessToken = useCookie("access_token").value;
|
||||
if (["/login", "/register"].includes(to.path)) {
|
||||
console.log("path is login or register");
|
||||
const apiBase = useCookie("api_base");
|
||||
console.log("apiBase gotten:", apiBase.value);
|
||||
if (!apiBase.value) {
|
||||
const requestUrl = useRequestURL();
|
||||
console.log("request url:", requestUrl.href);
|
||||
const apiVersion = useRuntimeConfig().public.apiVersion;
|
||||
console.log("api version:", apiVersion);
|
||||
console.log("apiBase not set");
|
||||
const { status, data: gorbTxt } = await useFetch(`${requestUrl.protocol}//${requestUrl.host}/.well-known/gorb.txt`, { responseType: "text" });
|
||||
if (status.value == "success" && gorbTxt.value) {
|
||||
console.log("got gorb.txt:", gorbTxt.value);
|
||||
const parsed = parseWellKnown(gorbTxt.value as string);
|
||||
if (parsed.ApiBaseUrl) {
|
||||
apiBase.value = `${parsed.ApiBaseUrl}/${apiVersion}`;
|
||||
console.log("set apiBase to:", parsed.ApiBaseUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (accessToken) {
|
||||
return await navigateTo("/");
|
||||
}
|
||||
|
|
|
@ -47,14 +47,17 @@ definePageMeta({
|
|||
layout: "auth"
|
||||
})
|
||||
|
||||
const instanceUrl = useCookie("instance_url").value;
|
||||
const registrationEnabled = ref<boolean>(false);
|
||||
const registrationEnabled = useState("registrationEnabled", () => true);
|
||||
|
||||
if (instanceUrl) {
|
||||
const statsUrl = new URL("/stats", instanceUrl).href;
|
||||
const { status, data, error } = await useFetch<StatsResponse>(statsUrl);
|
||||
console.log("wah");
|
||||
console.log("weoooo")
|
||||
const apiBase = useCookie("api_base");
|
||||
console.log("apiBase:", apiBase.value);
|
||||
if (apiBase.value) {
|
||||
const { status, data, error } = await useFetch<StatsResponse>(`${apiBase.value}/stats`);
|
||||
if (status.value == "success" && data.value) {
|
||||
registrationEnabled.value = data.value.registration_enabled;
|
||||
console.log("set registration enabled value to:", data.value.registration_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,12 +95,6 @@ const query = useRoute().query as Record<string, string>;
|
|||
const searchParams = new URLSearchParams(query);
|
||||
const loginUrl = `/login?${searchParams}`
|
||||
|
||||
onMounted(() => {
|
||||
if (auth.accessToken.value) {
|
||||
//return navigateTo(redirectTo ? redirectTo as string : useAppConfig().baseURL as string);
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
watch(() => form.username, (newValue) => {
|
||||
console.log("username change:", newValue);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue