Skip to content

Commit

Permalink
admin: Add entry token verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrRip committed Nov 8, 2023
1 parent 197d918 commit 7bd5a45
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default defineAppConfig({
appHomeBase: 'https://mzg.fan/',

backendBase: 'https://api.mzg.fan/v1',
backendProjectId: '649758e1eb1fa584a04d',

Expand Down
13 changes: 13 additions & 0 deletions packages/admin/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div />
</template>

<script setup lang="ts">
const route = useRoute()
if (route.query.entrytoken) {
const entryRouteQueries = { ...route.query }
delete entryRouteQueries.entrytoken
await navigateTo({ path: route.path, query: entryRouteQueries })
}
</script>
38 changes: 38 additions & 0 deletions packages/admin/middleware/entry.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Client, Databases, Query } from 'node-appwrite'

export default defineNuxtRouteMiddleware(async (to) => {
if (process.client) { return }

const entryTokenQuery = to.query.entrytoken
const entryTokenCookie = useCookie('admin_entry_token')
let entryToken = entryTokenQuery ?? entryTokenCookie.value
if (!String(entryToken).match(/[A-Za-z0-9]{32}/)) { entryToken = null }

function returnToHome () {
return navigateTo(useAppConfig().appHomeBase, { external: true })
}

const backendClient = new Client()
const backendDatabases = new Databases(backendClient)
backendClient.setEndpoint(useAppConfig().backendBase)
.setProject(useAppConfig().backendProjectId)
.setKey(useRuntimeConfig().backendApiKey)

if (entryToken) {
const entryTokenMatches = (await backendDatabases.listDocuments(
'admin',
'entry-tokens',
[
Query.equal('token', [String(entryToken)]),
Query.equal('valid', [true])
]
)).total
if (entryTokenMatches) {
entryTokenCookie.value = String(entryToken)
} else {
returnToHome()
}
} else {
returnToHome()
}
})

0 comments on commit 7bd5a45

Please sign in to comment.