Skip to content

Commit

Permalink
✨ bearer only
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 13, 2024
1 parent cff7e6f commit 5c6b4af
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default defineEventHandler(async (event) => {
'/api/token',
]

const bearer = (event.node.req.headers.authentication as string)?.split(' ')[1] || undefined
if (bearer && auth.verify(useRuntimeConfig(event), bearer) === true)
await auth.set(bearer)
const token = auth.bearer(event)
if (token && auth.verify(useRuntimeConfig(event), token) === true)
await auth.set(token)
if (gatedRoutes.some(route => getRequestURL(event).pathname.startsWith(route)))
if (!auth.user())
return metapi().error(event, 'Not Authenticated', 401)
Expand Down
3 changes: 1 addition & 2 deletions server/routes/token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { z } from 'zod'

const index = defineEventHandler(async (event) => {
console.log(parseCookies(event))
return metapi().render(
await prisma.$extends({
result: {
token: {
isCurrent: {
needs: { hash: true },
compute({ hash }) { return hash === parseCookies(event).token },
compute({ hash }) { return hash === auth.bearer(event) },
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions server/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { H3Event } from 'h3'
import type { Token } from '@prisma/client'
import type { RuntimeConfig } from 'nuxt/schema'
import type { User } from '~/types/models'

let token: Token & { user: User } | null = null

const bearer = (event: H3Event): string | undefined =>
(event.node.req.headers.authentication as string)?.split(' ')[1] || undefined

const set = async (hash: string): Promise<User> => {
// if (token?.user) return token.user
token = await prisma.token.findUnique({
Expand Down Expand Up @@ -37,4 +41,5 @@ export const auth = {
user,
hash,
clear,
bearer,
}

0 comments on commit 5c6b4af

Please sign in to comment.