chore: wip oauth

This commit is contained in:
Anthony Fu 2022-11-15 19:54:07 +08:00
parent 6755ed6f94
commit 6ea4879190
8 changed files with 75 additions and 10 deletions

6
constants/index.ts Normal file
View file

@ -0,0 +1,6 @@
export const APP_NAME = 'Nuxtodon'
export const HOST_DOMAIN = process.dev
? 'http://localhost:3000'
: 'https://nuxtodon.netlify.app'

View file

@ -28,6 +28,7 @@
"postcss-nested": "^6.0.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"sanitize-html": "^2.7.3",
"typescript": "^4.8.4"
"typescript": "^4.8.4",
"ufo": "^1.0.0"
}
}

View file

@ -1,6 +1,7 @@
<script setup lang="ts">
const token = useCookie('nuxtodon-token')
const router = useRouter()
// TODO: move to middleware
if (!token.value)
router.replace('/public')

View file

@ -1,10 +1,41 @@
<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"

View file

@ -1,13 +1,19 @@
import { login } from 'masto'
export const DEFAULT_SERVER = 'https://mas.to'
export const DEFAULT_SERVER = 'mas.to'
export default defineNuxtPlugin((nuxt) => {
const server = useCookie('nuxtodon-server')
const token = useCookie('nuxtodon-token')
const masto = login({
url: server.value || DEFAULT_SERVER,
url: `https://${server.value || DEFAULT_SERVER}`,
accessToken: token.value,
})
nuxt.vueApp.provide('masto', masto)
// Reload the page when the token changes
watch(token, () => {
location.reload()
})
})

View file

@ -20,6 +20,7 @@ specifiers:
rollup-plugin-node-polyfills: ^0.2.1
sanitize-html: ^2.7.3
typescript: ^4.8.4
ufo: ^1.0.0
devDependencies:
'@antfu/eslint-config': 0.30.1_rmayb2veg2btbq6mbmnyivgasy
@ -41,6 +42,7 @@ devDependencies:
rollup-plugin-node-polyfills: 0.2.1
sanitize-html: 2.7.3
typescript: 4.8.4
ufo: 1.0.0
packages:
@ -6559,6 +6561,10 @@ packages:
resolution: {integrity: sha512-fk6CmUgwKCfX79EzcDQQpSCMxrHstvbLswFChHS0Vump+kFkw7nJBfTZoC1j0bOGoY9I7R3n2DGek5ajbcYnOw==}
dev: true
/ufo/1.0.0:
resolution: {integrity: sha512-DRty0ZBNlJ2R59y4mEupJRKLbkLQsc4qtxjpQv78AwEDuBkaUogMc2LkeqW3HddFlw6NwnXYfdThEZOiNgkmmQ==}
dev: true
/ultrahtml/0.4.0:
resolution: {integrity: sha512-pnJXeIWo9gu7ftQLsMii4Se9kWOzyuH63EDsOsFKwP9XTdLG+QI+JUUxXFSAlCJ/frcdmjfE6kSvvCKiGmiakg==}
dev: true

View file

@ -0,0 +1,21 @@
import { getQuery } from 'ufo'
export default defineEventHandler(async (event) => {
const query = getQuery(event.req.url!)
const code = query.code
const server = event.context.params.server
console.log({ query, server })
const res = await $fetch(`https://${server}/oauth/token`, {
method: 'POST',
body: {
client_id: 'your_client_id_here',
client_secret: 'your_client_secret_here',
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'authorization_code',
code,
scope: 'read write follow push',
},
})
console.log({ res })
})

View file

@ -1,7 +0,0 @@
const startAt = Date.now()
let count = 0
export default defineEventHandler(() => ({
pageview: count++,
startAt,
}))