nikhildhoka
nikhildhoka
CCConvex Community
Created by nikhildhoka on 12/12/2024 in #support-community
corsHttpRouter
How can I get the code for the corsHttpRouter mentioned in this video? https://youtu.be/c7Y0wCml1kg?si=PPh7IPR_04Lvudg1
14 replies
CCConvex Community
Created by nikhildhoka on 12/8/2024 in #support-community
How to retrieve files from the Storage in Convex
code:
export const getPackageById = query({
args: { packageId: v.id("packageTable") }, // Validate that packageId is an ID from "packageTable"
handler: async (ctx: any, args: any) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Unauthorized");
}
const pkg = await ctx.db.get(args.packageId); // Fetch the package by ID
pkg.metadata.ID = pkg._id;
if (!pkg) {
throw new Error(`Package with ID ${args.packageId} not found.`);
}
const fileStorageId = (pkg.data.Content) ! as Id<"_storage">;
console.log('File Storage ID:', fileStorageId);
const blob = await ctx.storage.get(fileStorageId);
console.log("blob: ", blob);
if (!blob) {
return new Response("Blob not found", { status: 404 });
}
pkg.data.Content = await blobToBase64(blob);
return pkg;
},
});
export const getPackageById = query({
args: { packageId: v.id("packageTable") }, // Validate that packageId is an ID from "packageTable"
handler: async (ctx: any, args: any) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Unauthorized");
}
const pkg = await ctx.db.get(args.packageId); // Fetch the package by ID
pkg.metadata.ID = pkg._id;
if (!pkg) {
throw new Error(`Package with ID ${args.packageId} not found.`);
}
const fileStorageId = (pkg.data.Content) ! as Id<"_storage">;
console.log('File Storage ID:', fileStorageId);
const blob = await ctx.storage.get(fileStorageId);
console.log("blob: ", blob);
if (!blob) {
return new Response("Blob not found", { status: 404 });
}
pkg.data.Content = await blobToBase64(blob);
return pkg;
},
});
error: Dec 07, 19:53:04 Q queries/packageTable:getPackageById failure 33ms Uncaught TypeError: ctx.storage.get is not a function at handler (../../convex/queries/packageTable.ts:37:31)
7 replies
CCConvex Community
Created by nikhildhoka on 12/6/2024 in #support-community
Sign in flow not working on deployment on AWS Amplify
No description
29 replies
CCConvex Community
Created by nikhildhoka on 11/28/2024 in #support-community
/authenticate route to get the Bearer Token of the user given their username and pass using Clerk
How can I fix this httpAction to use Clerk to get the Bearer token of the user based on the username and password that the user will provide in the request body of the api call to convex:
import { httpAction } from "../_generated/server";
import { Clerk } from "@clerk/clerk-js";

export const authenticateHandler = httpAction(async (ctx, request) => {
try {
// Parse the request body
const { User, Secret } = await request.json();

// Authenticate the user using Clerk's users API
const user = await clerk.users.verifyPassword({ identifier: User, password: Secret });

if (user) {
// Create a session for the authenticated user
const session = await clerk.sessions.createSession({ userId: user.id });

// Retrieve the session token
const token = session.token;

// If authentication is successful, return the token
return new Response(JSON.stringify({
token: token,
}), { status: 200 });
}
} catch (error: any) {
if (error.message.includes("invalid credentials")) {
// Return 401 if the user or password is invalid
return new Response("The user or password is invalid.", { status: 401 });
}

// If any required fields are missing, return 400
if (error.message.includes("missing fields")) {
return new Response("There is missing field(s) in the AuthenticationRequest or it is formed improperly.", { status: 400 });
}

// Return 501 if the system does not support authentication
return new Response("This system does not support authentication.", { status: 501 });
}

// Ensure a Response is returned if no other conditions are met
return new Response("An unexpected error occurred.", { status: 500 });
// return new Response(JSON.stringify({messsage: "Hello world!"}), {
// status: 200,
// });
});
import { httpAction } from "../_generated/server";
import { Clerk } from "@clerk/clerk-js";

export const authenticateHandler = httpAction(async (ctx, request) => {
try {
// Parse the request body
const { User, Secret } = await request.json();

// Authenticate the user using Clerk's users API
const user = await clerk.users.verifyPassword({ identifier: User, password: Secret });

if (user) {
// Create a session for the authenticated user
const session = await clerk.sessions.createSession({ userId: user.id });

// Retrieve the session token
const token = session.token;

// If authentication is successful, return the token
return new Response(JSON.stringify({
token: token,
}), { status: 200 });
}
} catch (error: any) {
if (error.message.includes("invalid credentials")) {
// Return 401 if the user or password is invalid
return new Response("The user or password is invalid.", { status: 401 });
}

// If any required fields are missing, return 400
if (error.message.includes("missing fields")) {
return new Response("There is missing field(s) in the AuthenticationRequest or it is formed improperly.", { status: 400 });
}

// Return 501 if the system does not support authentication
return new Response("This system does not support authentication.", { status: 501 });
}

// Ensure a Response is returned if no other conditions are met
return new Response("An unexpected error occurred.", { status: 500 });
// return new Response(JSON.stringify({messsage: "Hello world!"}), {
// status: 200,
// });
});
5 replies
CCConvex Community
Created by nikhildhoka on 11/25/2024 in #support-community
Backend URL Vercel
What would be the Backend URL for a Convex application deployed on Vercel?
17 replies
CCConvex Community
Created by nikhildhoka on 11/19/2024 in #support-community
How to add a dynamic route to the httpRouter
I am trying to create an endpoint in the form /package/{packageId} and this should return all the details for that specific package. How to create such a route to the Http router in convex?
4 replies