feat: basic oauth
This commit is contained in:
parent
72b13f5265
commit
7ab17001f0
16 changed files with 199 additions and 106 deletions
|
@ -1,19 +1,11 @@
|
|||
import { login } from 'masto'
|
||||
|
||||
export const DEFAULT_SERVER = 'mas.to'
|
||||
|
||||
export default defineNuxtPlugin((nuxt) => {
|
||||
const server = useCookie('nuxtodon-server')
|
||||
const token = useCookie('nuxtodon-token')
|
||||
const { server, token } = useAppCookies()
|
||||
|
||||
const masto = login({
|
||||
url: `https://${server.value || DEFAULT_SERVER}`,
|
||||
url: `https://${server.value}`,
|
||||
accessToken: token.value,
|
||||
})
|
||||
nuxt.vueApp.provide('masto', masto)
|
||||
|
||||
// Reload the page when the token changes
|
||||
watch(token, () => {
|
||||
location.reload()
|
||||
})
|
||||
})
|
||||
|
|
47
plugins/store.client.ts
Normal file
47
plugins/store.client.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { login as loginMasto } from 'masto'
|
||||
import type { UserLogin } from '~/types'
|
||||
|
||||
function createStore() {
|
||||
const { server, token } = useAppCookies()
|
||||
const accounts = useLocalStorage<UserLogin[]>('nuxtodon-accounts', [], { deep: true })
|
||||
const currentIndex = useLocalStorage<number>('nuxtodon-current-user', -1)
|
||||
const currentUser = computed<UserLogin | undefined>(() => accounts.value[currentIndex.value])
|
||||
|
||||
async function login(user: UserLogin) {
|
||||
const existing = accounts.value.findIndex(u => u.server === user.server && u.token === user.token)
|
||||
if (existing !== -1) {
|
||||
if (currentIndex.value === existing)
|
||||
return null
|
||||
currentIndex.value = existing
|
||||
server.value = user.server
|
||||
token.value = user.token
|
||||
return true
|
||||
}
|
||||
|
||||
const masto = await loginMasto({
|
||||
url: `https://${user.server}`,
|
||||
accessToken: user.token,
|
||||
})
|
||||
const me = await masto.accounts.verifyCredentials()
|
||||
user.account = me
|
||||
|
||||
accounts.value.push(user)
|
||||
currentIndex.value = accounts.value.length
|
||||
server.value = user.server
|
||||
token.value = user.token
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return {
|
||||
currentUser,
|
||||
accounts,
|
||||
login,
|
||||
}
|
||||
}
|
||||
|
||||
export type AppStore = ReturnType<typeof createStore>
|
||||
|
||||
export default defineNuxtPlugin((nuxt) => {
|
||||
nuxt.vueApp.provide('app-store', createStore())
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue