authenticated endpoints
This commit is contained in:
parent
8126f118be
commit
de4f4f6519
4 changed files with 32 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ import { v } from 'convex/values';
|
||||||
import { providerValidator } from './schema';
|
import { providerValidator } from './schema';
|
||||||
import * as array from '../../utils/array';
|
import * as array from '../../utils/array';
|
||||||
import * as object from '../../utils/object';
|
import * as object from '../../utils/object';
|
||||||
|
import { internal } from './_generated/api';
|
||||||
|
|
||||||
export const get_enabled = query({
|
export const get_enabled = query({
|
||||||
args: {
|
args: {
|
||||||
|
|
@ -42,8 +43,17 @@ export const set = mutation({
|
||||||
model_id: v.string(),
|
model_id: v.string(),
|
||||||
user_id: v.string(),
|
user_id: v.string(),
|
||||||
enabled: v.boolean(),
|
enabled: v.boolean(),
|
||||||
|
session_token: v.string(),
|
||||||
},
|
},
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const session = await ctx.runQuery(internal.betterAuth.getSession, {
|
||||||
|
sessionToken: args.session_token,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
const existing = await ctx.db
|
const existing = await ctx.db
|
||||||
.query('user_enabled_models')
|
.query('user_enabled_models')
|
||||||
.withIndex('by_model_provider', (q) =>
|
.withIndex('by_model_provider', (q) =>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { v } from 'convex/values';
|
import { v } from 'convex/values';
|
||||||
import { Provider } from '../../types';
|
import { Provider } from '../../types';
|
||||||
|
import { internal } from './_generated/api';
|
||||||
import { mutation, query } from './_generated/server';
|
import { mutation, query } from './_generated/server';
|
||||||
import { providerValidator } from './schema';
|
import { providerValidator } from './schema';
|
||||||
|
|
||||||
|
|
@ -27,8 +28,17 @@ export const get = query({
|
||||||
args: {
|
args: {
|
||||||
user_id: v.string(),
|
user_id: v.string(),
|
||||||
provider: providerValidator,
|
provider: providerValidator,
|
||||||
|
session_token: v.string(),
|
||||||
},
|
},
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const session = await ctx.runQuery(internal.betterAuth.getSession, {
|
||||||
|
sessionToken: args.session_token,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
const key = await ctx.db
|
const key = await ctx.db
|
||||||
.query('user_keys')
|
.query('user_keys')
|
||||||
.withIndex('by_provider_user', (q) =>
|
.withIndex('by_provider_user', (q) =>
|
||||||
|
|
@ -45,8 +55,17 @@ export const set = mutation({
|
||||||
provider: providerValidator,
|
provider: providerValidator,
|
||||||
user_id: v.string(),
|
user_id: v.string(),
|
||||||
key: v.string(),
|
key: v.string(),
|
||||||
|
session_token: v.string(),
|
||||||
},
|
},
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
const session = await ctx.runQuery(internal.betterAuth.getSession, {
|
||||||
|
sessionToken: args.session_token,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
const existing = await ctx.db
|
const existing = await ctx.db
|
||||||
.query('user_keys')
|
.query('user_keys')
|
||||||
.withIndex('by_provider_user', (q) =>
|
.withIndex('by_provider_user', (q) =>
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
const keyQuery = useCachedQuery(api.user_keys.get, {
|
const keyQuery = useCachedQuery(api.user_keys.get, {
|
||||||
user_id: session.current?.user.id ?? '',
|
user_id: session.current?.user.id ?? '',
|
||||||
provider,
|
provider,
|
||||||
|
session_token: session.current?.session.token ?? '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const client = useConvexClient();
|
const client = useConvexClient();
|
||||||
|
|
@ -44,6 +45,7 @@
|
||||||
provider,
|
provider,
|
||||||
user_id: session.current?.user.id ?? '',
|
user_id: session.current?.user.id ?? '',
|
||||||
key: `${key}`,
|
key: `${key}`,
|
||||||
|
session_token: session.current?.session.token,
|
||||||
}),
|
}),
|
||||||
(e) => e
|
(e) => e
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@
|
||||||
user_id: session.current.user.id,
|
user_id: session.current.user.id,
|
||||||
model_id: model.id,
|
model_id: model.id,
|
||||||
enabled: v,
|
enabled: v,
|
||||||
|
session_token: session.current?.session.token,
|
||||||
}),
|
}),
|
||||||
(e) => e
|
(e) => e
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue