forked from Mirrors/elk
chore: wip oauth
This commit is contained in:
parent
6755ed6f94
commit
6ea4879190
8 changed files with 75 additions and 10 deletions
6
constants/index.ts
Normal file
6
constants/index.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export const APP_NAME = 'Nuxtodon'
|
||||||
|
|
||||||
|
export const HOST_DOMAIN = process.dev
|
||||||
|
? 'http://localhost:3000'
|
||||||
|
: 'https://nuxtodon.netlify.app'
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
"postcss-nested": "^6.0.0",
|
"postcss-nested": "^6.0.0",
|
||||||
"rollup-plugin-node-polyfills": "^0.2.1",
|
"rollup-plugin-node-polyfills": "^0.2.1",
|
||||||
"sanitize-html": "^2.7.3",
|
"sanitize-html": "^2.7.3",
|
||||||
"typescript": "^4.8.4"
|
"typescript": "^4.8.4",
|
||||||
|
"ufo": "^1.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const token = useCookie('nuxtodon-token')
|
const token = useCookie('nuxtodon-token')
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
// TODO: move to middleware
|
||||||
if (!token.value)
|
if (!token.value)
|
||||||
router.replace('/public')
|
router.replace('/public')
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,41 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { login } from 'masto'
|
||||||
|
import { DEFAULT_SERVER } from '~/plugins/masto'
|
||||||
|
|
||||||
const server = useCookie('nuxtodon-server')
|
const server = useCookie('nuxtodon-server')
|
||||||
const token = useCookie('nuxtodon-token')
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div p4>
|
<div p4>
|
||||||
|
<button @click="oauth()">
|
||||||
|
OAuth
|
||||||
|
</button>
|
||||||
<input
|
<input
|
||||||
v-model="server"
|
v-model="server"
|
||||||
placeholder="Server URL"
|
placeholder="Server URL"
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
import { login } from 'masto'
|
import { login } from 'masto'
|
||||||
|
|
||||||
export const DEFAULT_SERVER = 'https://mas.to'
|
export const DEFAULT_SERVER = 'mas.to'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxt) => {
|
export default defineNuxtPlugin((nuxt) => {
|
||||||
const server = useCookie('nuxtodon-server')
|
const server = useCookie('nuxtodon-server')
|
||||||
const token = useCookie('nuxtodon-token')
|
const token = useCookie('nuxtodon-token')
|
||||||
|
|
||||||
const masto = login({
|
const masto = login({
|
||||||
url: server.value || DEFAULT_SERVER,
|
url: `https://${server.value || DEFAULT_SERVER}`,
|
||||||
accessToken: token.value,
|
accessToken: token.value,
|
||||||
})
|
})
|
||||||
nuxt.vueApp.provide('masto', masto)
|
nuxt.vueApp.provide('masto', masto)
|
||||||
|
|
||||||
|
// Reload the page when the token changes
|
||||||
|
watch(token, () => {
|
||||||
|
location.reload()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -20,6 +20,7 @@ specifiers:
|
||||||
rollup-plugin-node-polyfills: ^0.2.1
|
rollup-plugin-node-polyfills: ^0.2.1
|
||||||
sanitize-html: ^2.7.3
|
sanitize-html: ^2.7.3
|
||||||
typescript: ^4.8.4
|
typescript: ^4.8.4
|
||||||
|
ufo: ^1.0.0
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@antfu/eslint-config': 0.30.1_rmayb2veg2btbq6mbmnyivgasy
|
'@antfu/eslint-config': 0.30.1_rmayb2veg2btbq6mbmnyivgasy
|
||||||
|
@ -41,6 +42,7 @@ devDependencies:
|
||||||
rollup-plugin-node-polyfills: 0.2.1
|
rollup-plugin-node-polyfills: 0.2.1
|
||||||
sanitize-html: 2.7.3
|
sanitize-html: 2.7.3
|
||||||
typescript: 4.8.4
|
typescript: 4.8.4
|
||||||
|
ufo: 1.0.0
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
|
@ -6559,6 +6561,10 @@ packages:
|
||||||
resolution: {integrity: sha512-fk6CmUgwKCfX79EzcDQQpSCMxrHstvbLswFChHS0Vump+kFkw7nJBfTZoC1j0bOGoY9I7R3n2DGek5ajbcYnOw==}
|
resolution: {integrity: sha512-fk6CmUgwKCfX79EzcDQQpSCMxrHstvbLswFChHS0Vump+kFkw7nJBfTZoC1j0bOGoY9I7R3n2DGek5ajbcYnOw==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/ufo/1.0.0:
|
||||||
|
resolution: {integrity: sha512-DRty0ZBNlJ2R59y4mEupJRKLbkLQsc4qtxjpQv78AwEDuBkaUogMc2LkeqW3HddFlw6NwnXYfdThEZOiNgkmmQ==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/ultrahtml/0.4.0:
|
/ultrahtml/0.4.0:
|
||||||
resolution: {integrity: sha512-pnJXeIWo9gu7ftQLsMii4Se9kWOzyuH63EDsOsFKwP9XTdLG+QI+JUUxXFSAlCJ/frcdmjfE6kSvvCKiGmiakg==}
|
resolution: {integrity: sha512-pnJXeIWo9gu7ftQLsMii4Se9kWOzyuH63EDsOsFKwP9XTdLG+QI+JUUxXFSAlCJ/frcdmjfE6kSvvCKiGmiakg==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
21
server/api/[server]/oauth.ts
Normal file
21
server/api/[server]/oauth.ts
Normal 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 })
|
||||||
|
})
|
|
@ -1,7 +0,0 @@
|
||||||
const startAt = Date.now()
|
|
||||||
let count = 0
|
|
||||||
|
|
||||||
export default defineEventHandler(() => ({
|
|
||||||
pageview: count++,
|
|
||||||
startAt,
|
|
||||||
}))
|
|
Loading…
Reference in a new issue