diff --git a/composables/users.ts b/composables/users.ts index ef090594..416d1b0e 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -1,7 +1,7 @@ import { login as loginMasto } from 'masto' -import type { Account, AccountCredentials, Instance, WsEvents } from 'masto' +import type { Account, AccountCredentials, Instance, MastoClient, WsEvents } from 'masto' import type { Ref } from 'vue' -import type { UserLogin } from '~/types' +import type { ElkMasto, UserLogin } from '~/types' import { DEFAULT_POST_CHARS_LIMIT, DEFAULT_SERVER, @@ -43,7 +43,7 @@ export const useUsers = () => users export const characterLimit = computed(() => currentInstance.value?.configuration.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT) -export async function loginTo(user?: Omit & { account?: AccountCredentials }) { +async function loginTo(user?: Omit & { account?: AccountCredentials }) { const config = useRuntimeConfig() const route = useRoute() const router = useRouter() @@ -246,3 +246,46 @@ export function clearUserLocalStorage(account?: Account) { delete storage.value[id] }) } + +export const createMasto = () => { + const api = shallowRef(null) + const apiPromise = ref | null>(null) + const initialised = computed(() => !!api.value) + + const masto = new Proxy({} as ElkMasto, { + get(_, key: keyof ElkMasto) { + if (key === 'loggedIn') + return initialised + + if (key === 'loginTo') { + return (...args: any[]): Promise => { + return apiPromise.value = loginTo(...args).then((r) => { + api.value = r + return masto + }).catch(() => { + // Show error page when Mastodon server is down + throw createError({ + fatal: true, + statusMessage: 'Could not log into account.', + }) + }) + } + } + + if (api.value && key in api.value) + return api.value[key as keyof MastoClient] + + if (!api.value) { + return new Proxy({}, { + get(_, subkey) { + return (...args: any[]) => apiPromise.value?.then((r: any) => r[key][subkey](...args)) + }, + }) + } + + return undefined + }, + }) + + return masto +} diff --git a/middleware/auth.ts b/middleware/auth.ts index dad20976..4c251eb7 100644 --- a/middleware/auth.ts +++ b/middleware/auth.ts @@ -1,7 +1,7 @@ export default defineNuxtRouteMiddleware((to) => { if (process.server) return - if (!currentUser.value) + if (!currentUser.value && to.path !== '/signin/callback') return navigateTo(`/${currentServer.value}/public`) if (to.path === '/') return navigateTo('/home') diff --git a/plugins/masto.ts b/plugins/masto.ts index e2540fc6..1fd0ac5b 100644 --- a/plugins/masto.ts +++ b/plugins/masto.ts @@ -1,45 +1,5 @@ -import type { MastoClient } from 'masto' -import type { ElkMasto } from '~/types' - export default defineNuxtPlugin(async (nuxtApp) => { - const api = shallowRef(null) - const apiPromise = ref | null>(null) - const initialised = computed(() => !!api.value) - - const masto = new Proxy({} as ElkMasto, { - get(_, key: keyof ElkMasto) { - if (key === 'loggedIn') - return initialised - - if (key === 'loginTo') { - return (...args: any[]): Promise => { - return apiPromise.value = loginTo(...args).then((r) => { - api.value = r - return masto - }).catch(() => { - // Show error page when Mastodon server is down - throw createError({ - fatal: true, - statusMessage: 'Could not log into account.', - }) - }) - } - } - - if (api.value && key in api.value) - return api.value[key as keyof MastoClient] - - if (!api.value) { - return new Proxy({}, { - get(_, subkey) { - return (...args: any[]) => apiPromise.value?.then((r: any) => r[key][subkey](...args)) - }, - }) - } - - return undefined - }, - }) + const masto = createMasto() if (process.client) { const { query } = useRoute()