Skip to content

Commit

Permalink
fix: don't apply auth middleware on signin callback (elk-zone#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Dec 20, 2022
1 parent 2e97780 commit dfc2437
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
49 changes: 46 additions & 3 deletions composables/users.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<UserLogin, 'account'> & { account?: AccountCredentials }) {
async function loginTo(user?: Omit<UserLogin, 'account'> & { account?: AccountCredentials }) {
const config = useRuntimeConfig()
const route = useRoute()
const router = useRouter()
Expand Down Expand Up @@ -246,3 +246,46 @@ export function clearUserLocalStorage(account?: Account) {
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
}
2 changes: 1 addition & 1 deletion middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
42 changes: 1 addition & 41 deletions plugins/masto.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
import type { MastoClient } from 'masto'
import type { ElkMasto } from '~/types'

export default defineNuxtPlugin(async (nuxtApp) => {
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
},
})
const masto = createMasto()

if (process.client) {
const { query } = useRoute()
Expand Down

0 comments on commit dfc2437

Please sign in to comment.