1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-23 22:56:09 +09:00
elk/plugins/masto.ts
2022-11-26 20:33:36 +01:00

33 lines
776 B
TypeScript

import { login } from 'masto'
import { currentUser } from '../composables/users'
import { DEFAULT_SERVER } from '~/constants'
export default defineNuxtPlugin(async () => {
try {
const accessToken = currentUser.value?.token
// TODO: improve upstream to make this synchronous (delayed auth)
const masto = await login({
url: `https://${currentUser.value?.server || DEFAULT_SERVER}`,
accessToken,
})
if (accessToken)
masto.accounts.verifyCredentials().catch(() => signout())
return {
provide: {
masto,
},
}
}
catch {
// TODO: handle error
// Show error page when Mastodon server is down
throw createError({
fatal: true,
statusMessage: 'Could not log into account.',
})
}
})