fix: don't apply auth middleware on signin callback (#478)

This commit is contained in:
Daniel Roe 2022-12-20 21:03:15 +00:00 committed by GitHub
parent 2e97780899
commit dfc24370d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 45 deletions

View file

@ -1,7 +1,7 @@
import { login as loginMasto } from 'masto' 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 { Ref } from 'vue'
import type { UserLogin } from '~/types' import type { ElkMasto, UserLogin } from '~/types'
import { import {
DEFAULT_POST_CHARS_LIMIT, DEFAULT_POST_CHARS_LIMIT,
DEFAULT_SERVER, DEFAULT_SERVER,
@ -43,7 +43,7 @@ export const useUsers = () => users
export const characterLimit = computed(() => currentInstance.value?.configuration.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT) export const characterLimit = computed(() => currentInstance.value?.configuration.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT)
export async function loginTo(user?: Omit<UserLogin, 'account'> & { account?: AccountCredentials }) { async function loginTo(user?: Omit<UserLogin, 'account'> & { account?: AccountCredentials }) {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
@ -246,3 +246,46 @@ export function clearUserLocalStorage(account?: Account) {
delete storage.value[id] delete storage.value[id]
}) })
} }
export const createMasto = () => {
const api = shallowRef<MastoClient | null>(null)
const apiPromise = ref<Promise<MastoClient> | 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<MastoClient> => {
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
}

View file

@ -1,7 +1,7 @@
export default defineNuxtRouteMiddleware((to) => { export default defineNuxtRouteMiddleware((to) => {
if (process.server) if (process.server)
return return
if (!currentUser.value) if (!currentUser.value && to.path !== '/signin/callback')
return navigateTo(`/${currentServer.value}/public`) return navigateTo(`/${currentServer.value}/public`)
if (to.path === '/') if (to.path === '/')
return navigateTo('/home') return navigateTo('/home')

View file

@ -1,45 +1,5 @@
import type { MastoClient } from 'masto'
import type { ElkMasto } from '~/types'
export default defineNuxtPlugin(async (nuxtApp) => { export default defineNuxtPlugin(async (nuxtApp) => {
const api = shallowRef<MastoClient | null>(null) const masto = createMasto()
const apiPromise = ref<Promise<MastoClient> | 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<MastoClient> => {
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
},
})
if (process.client) { if (process.client) {
const { query } = useRoute() const { query } = useRoute()