feat: basic oauth

This commit is contained in:
Anthony Fu 2022-11-15 23:48:23 +08:00
parent 72b13f5265
commit 7ab17001f0
16 changed files with 199 additions and 106 deletions

View file

@ -1,54 +0,0 @@
<script setup lang="ts">
import { login } from 'masto'
import { DEFAULT_SERVER } from '~/plugins/masto'
const server = useCookie('nuxtodon-server')
const token = useCookie('nuxtodon-token')
async function oauth() {
const client = await login({
url: `https://${server.value || DEFAULT_SERVER}`,
})
const redirectUri = `${location.origin}/api/${server.value || DEFAULT_SERVER}/oauth`
const app = await client.apps.create({
clientName: 'Nuxtodon',
redirectUris: redirectUri,
scopes: 'read write follow push',
})
console.log({ app })
const url = `https://${server.value || DEFAULT_SERVER}/oauth/authorize
?client_id=${app.clientId}
&scope=read+write+follow+push
&redirect_uri=${encodeURIComponent(redirectUri)}
&response_type=code`.replace(/\n/g, '')
const a = document.createElement('a')
a.href = url
a.target = '_blank'
a.click()
}
</script>
<template>
<div p4>
<button @click="oauth()">
OAuth
</button>
<input
v-model="server"
placeholder="Server URL"
bg-transparent text-current
border="~ border" p="x2 y1" w-full
outline-none
>
<input
v-model="token"
placeholder="Token"
bg-transparent text-current
border="~ border" p="x2 y1" w-full
outline-none
>
</div>
</template>

17
pages/login/callback.vue Normal file
View file

@ -0,0 +1,17 @@
<script setup lang="ts">
const { query } = useRoute()
onMounted(async () => {
const { login } = useAppStore()
await login(query as any)
await nextTick()
await nextTick()
location.pathname = '/'
})
</script>
<template>
<div>
Login...
</div>
</template>

32
pages/login/index.vue Normal file
View file

@ -0,0 +1,32 @@
<script setup lang="ts">
const { server, token } = useAppCookies()
async function oauth() {
const a = document.createElement('a')
a.href = `/api/${server.value}/login`
a.target = '_blank'
a.click()
}
</script>
<template>
<div p4>
<button @click="oauth()">
OAuth
</button>
<input
v-model="server"
placeholder="Server URL"
bg-transparent text-current
border="~ border" p="x2 y1" w-full
outline-none
>
<input
v-model="token"
placeholder="Token"
bg-transparent text-current
border="~ border" p="x2 y1" w-full
outline-none
>
</div>
</template>