2023-01-03 09:39:00 +00:00
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
2022-12-20 08:20:40 +00:00
|
|
|
if (process.server)
|
|
|
|
return
|
|
|
|
|
2022-12-20 15:56:54 +00:00
|
|
|
const masto = useMasto()
|
|
|
|
|
2022-12-04 19:56:33 +00:00
|
|
|
// Skip running middleware before masto has been initialised
|
2022-12-20 15:56:54 +00:00
|
|
|
if (!masto)
|
2022-12-04 19:56:33 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if (!('server' in to.params))
|
|
|
|
return
|
|
|
|
|
2022-12-11 23:30:26 +00:00
|
|
|
const user = currentUser.value
|
|
|
|
|
2022-12-04 19:56:33 +00:00
|
|
|
// Handle redirecting to new permalink structure for users with old links
|
|
|
|
if (!to.params.server) {
|
|
|
|
return {
|
|
|
|
...to,
|
|
|
|
params: {
|
|
|
|
...to.params,
|
2022-12-11 23:30:26 +00:00
|
|
|
server: user.server,
|
2022-12-04 19:56:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 09:39:00 +00:00
|
|
|
// No need to additionally resolve an id if we're already logged in
|
|
|
|
if (user.server === to.params.server)
|
|
|
|
return
|
2022-12-04 19:56:33 +00:00
|
|
|
|
2023-01-03 09:39:00 +00:00
|
|
|
masto.loginTo({
|
|
|
|
server: to.params.server as string,
|
|
|
|
})
|
2022-12-04 19:56:33 +00:00
|
|
|
})
|