2022-11-27 15:13:04 +00:00
|
|
|
import type { MastoClient } from 'masto'
|
2022-11-26 15:42:58 +00:00
|
|
|
import { currentUser } from '../composables/users'
|
|
|
|
|
|
|
|
export default defineNuxtPlugin(async () => {
|
|
|
|
try {
|
2022-11-27 15:13:04 +00:00
|
|
|
const { query } = useRoute()
|
|
|
|
const user = typeof query.server === 'string' && typeof query.token === 'string'
|
|
|
|
? { server: query.server, token: query.token }
|
|
|
|
: currentUser.value
|
2022-11-26 19:33:36 +00:00
|
|
|
|
2022-11-26 15:42:58 +00:00
|
|
|
// TODO: improve upstream to make this synchronous (delayed auth)
|
2022-11-27 15:13:04 +00:00
|
|
|
const masto = await loginTo(user) as MastoClient
|
2022-11-26 19:33:36 +00:00
|
|
|
|
2022-11-26 15:42:58 +00:00
|
|
|
return {
|
|
|
|
provide: {
|
2022-11-27 15:13:04 +00:00
|
|
|
masto: shallowReactive({
|
|
|
|
replace(api: MastoClient) { this.api = api },
|
|
|
|
api: masto,
|
|
|
|
}),
|
2022-11-26 15:42:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
// TODO: handle error
|
|
|
|
// Show error page when Mastodon server is down
|
|
|
|
throw createError({
|
|
|
|
fatal: true,
|
|
|
|
statusMessage: 'Could not log into account.',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|