feat: avoid reloading the page when changing accounts (#180)
This commit is contained in:
parent
e2000321c5
commit
729b36a606
5
app.vue
5
app.vue
|
@ -10,13 +10,16 @@ useHead({
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// We want to trigger rerendering the page when account changes
|
||||||
|
const key = computed(() => useMasto().instances.config.url || 'default')
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-expressions
|
// eslint-disable-next-line no-unused-expressions
|
||||||
isDark.value
|
isDark.value
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<NuxtLoadingIndicator color="repeating-linear-gradient(to right,var(--c-primary) 0%,var(--c-primary-active) 100%)" />
|
<NuxtLoadingIndicator color="repeating-linear-gradient(to right,var(--c-primary) 0%,var(--c-primary-active) 100%)" />
|
||||||
<NuxtLayout>
|
<NuxtLayout :key="key">
|
||||||
<NuxtPage />
|
<NuxtPage />
|
||||||
</NuxtLayout>
|
</NuxtLayout>
|
||||||
<TeleportTarget
|
<TeleportTarget
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
export const useMasto = () => useNuxtApp().$masto
|
import type { MastoClient } from 'masto'
|
||||||
|
|
||||||
|
export const useMasto = () => useNuxtApp().$masto.api
|
||||||
|
|
||||||
|
export const setMasto = (masto: MastoClient) => {
|
||||||
|
useNuxtApp().$masto?.replace(masto)
|
||||||
|
}
|
||||||
|
|
|
@ -27,28 +27,37 @@ export const currentInstance = computed<null | Instance>(() => currentUserId.val
|
||||||
|
|
||||||
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: UserLogin & { account?: AccountCredentials }) {
|
export async function loginTo(user?: Omit<UserLogin, 'account'> & { account?: AccountCredentials }) {
|
||||||
|
if (user) {
|
||||||
const existing = users.value.find(u => u.server === user.server && u.token === user.token)
|
const existing = users.value.find(u => u.server === user.server && u.token === user.token)
|
||||||
if (existing) {
|
if (existing && currentUserId.value !== user.account?.id)
|
||||||
if (currentUserId.value === existing.account?.id)
|
|
||||||
return null
|
|
||||||
currentUserId.value = user.account?.id
|
currentUserId.value = user.account?.id
|
||||||
await reloadPage()
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const masto = await loginMasto({
|
const masto = await loginMasto({
|
||||||
url: `https://${user.server}`,
|
url: `https://${user?.server || DEFAULT_SERVER}`,
|
||||||
accessToken: user.token,
|
accessToken: user?.token,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (user?.token) {
|
||||||
|
try {
|
||||||
const me = await masto.accounts.verifyCredentials()
|
const me = await masto.accounts.verifyCredentials()
|
||||||
user.account = me
|
user.account = me
|
||||||
|
|
||||||
users.value.push(user)
|
if (!users.value.some(u => u.server === user.server && u.token === user.token))
|
||||||
|
users.value.push(user as UserLogin)
|
||||||
|
|
||||||
currentUserId.value = me.id
|
currentUserId.value = me.id
|
||||||
servers.value[me.id] = await masto.instances.fetch()
|
servers.value[me.id] = await masto.instances.fetch()
|
||||||
await reloadPage()
|
}
|
||||||
return true
|
catch {
|
||||||
|
await signout()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setMasto(masto)
|
||||||
|
|
||||||
|
return masto
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function signout() {
|
export async function signout() {
|
||||||
|
@ -72,10 +81,8 @@ export async function signout() {
|
||||||
// Set currentUserId to next user if available
|
// Set currentUserId to next user if available
|
||||||
currentUserId.value = users.value[0]?.account?.id
|
currentUserId.value = users.value[0]?.account?.id
|
||||||
|
|
||||||
await reloadPage()
|
if (!currentUserId.value)
|
||||||
}
|
await useRouter().push('/public')
|
||||||
|
|
||||||
export async function reloadPage(path = '/') {
|
await loginTo(currentUser.value)
|
||||||
await nextTick()
|
|
||||||
location.pathname = path
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
middleware: 'auth',
|
middleware: 'auth',
|
||||||
|
alias: ['/signin/callback'],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (useRoute().path === '/signin/callback') {
|
||||||
|
// This only cleans up the URL; page content should stay the same
|
||||||
|
useRouter().push('/home')
|
||||||
|
}
|
||||||
|
|
||||||
const paginator = useMasto().timelines.getHomeIterable()
|
const paginator = useMasto().timelines.getHomeIterable()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
definePageMeta({
|
|
||||||
layout: 'none',
|
|
||||||
})
|
|
||||||
|
|
||||||
const router = useRouter()
|
|
||||||
const { query } = useRoute()
|
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
await loginTo(query as any)
|
|
||||||
router.push('/')
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div h-full flex>
|
|
||||||
<div ma>
|
|
||||||
Login...
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
|
@ -1,23 +1,22 @@
|
||||||
import { login } from 'masto'
|
import type { MastoClient } from 'masto'
|
||||||
import { currentUser } from '../composables/users'
|
import { currentUser } from '../composables/users'
|
||||||
import { DEFAULT_SERVER } from '~/constants'
|
|
||||||
|
|
||||||
export default defineNuxtPlugin(async () => {
|
export default defineNuxtPlugin(async () => {
|
||||||
try {
|
try {
|
||||||
const accessToken = currentUser.value?.token
|
const { query } = useRoute()
|
||||||
|
const user = typeof query.server === 'string' && typeof query.token === 'string'
|
||||||
|
? { server: query.server, token: query.token }
|
||||||
|
: currentUser.value
|
||||||
|
|
||||||
// TODO: improve upstream to make this synchronous (delayed auth)
|
// TODO: improve upstream to make this synchronous (delayed auth)
|
||||||
const masto = await login({
|
const masto = await loginTo(user) as MastoClient
|
||||||
url: `https://${currentUser.value?.server || DEFAULT_SERVER}`,
|
|
||||||
accessToken,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (accessToken)
|
|
||||||
masto.accounts.verifyCredentials().catch(() => signout())
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
provide: {
|
provide: {
|
||||||
masto,
|
masto: shallowReactive({
|
||||||
|
replace(api: MastoClient) { this.api = api },
|
||||||
|
api: masto,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue