mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
feat[convex]: api key auth support
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import type { DataModel } from "@fileone/convex/dataModel"
|
||||
import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
|
||||
import { mutation, query } from "@fileone/convex/server"
|
||||
import type {
|
||||
DocumentByName,
|
||||
TableNamesInDataModel,
|
||||
UserIdentity,
|
||||
} from "convex/server"
|
||||
import type { GenericId } from "convex/values"
|
||||
import { type GenericId, v } from "convex/values"
|
||||
import {
|
||||
customCtx,
|
||||
customMutation,
|
||||
customQuery,
|
||||
} from "convex-helpers/server/customFunctions"
|
||||
import type { DataModel } from "@fileone/convex/dataModel"
|
||||
import type { MutationCtx, QueryCtx } from "@fileone/convex/server"
|
||||
import { mutation, query } from "@fileone/convex/server"
|
||||
import * as ApiKey from "./model/apikey"
|
||||
import { type AuthUser, userIdentityOrThrow, userOrThrow } from "./model/user"
|
||||
import * as Err from "./shared/error"
|
||||
|
||||
export type AuthenticatedQueryCtx = QueryCtx & {
|
||||
user: AuthUser
|
||||
@@ -50,6 +52,21 @@ export const authenticatedMutation = customMutation(
|
||||
}),
|
||||
)
|
||||
|
||||
/**
|
||||
* Custom mutation that requires api key authentication for a mutation.
|
||||
*/
|
||||
export const apiKeyAuthenticatedMutation = customMutation(mutation, {
|
||||
args: {
|
||||
apiKey: v.string(),
|
||||
},
|
||||
input: async (ctx, args) => {
|
||||
if (!(await ApiKey.verifyApiKey(ctx, args.apiKey))) {
|
||||
throw Err.create(Err.Code.Unauthenticated, "Invalid API key")
|
||||
}
|
||||
return { ctx, args }
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* Gets a document by its id and checks if the user is authorized to access it
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user