#getUserId

7 messages · Page 1 of 1 (latest)

heady ocean
#

'getUserId' est déprécié.ts(6385)
qu'est-ce qui remplace 'getUserId' ?

frail glacierBOT
#

Hi @heady ocean. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

getUserId est déprécié dans plusieurs contextes, et la “remplacement” dépend de ce que vous utilisez :

1. Avec Convex Auth (@convex-dev/auth)

Utilisez la fonction utilitaire typée :

import { getAuthUserId } from "@convex-dev/auth/server";

export const doSomething = mutation({
  args: {/* ... */},
  handler: async (ctx, args) => {
    const userId = await getAuthUserId(ctx);
    if (userId === null) {
      throw new Error("Client is not authenticated!");
    }
    const user = await ctx.db.get(userId);
    // ...
  },
});

getAuthUserId(ctx) renvoie Id<"users"> | null. [getAuthUserId]

2. Avec Better Auth (@convex-dev/better-auth)

Depuis la 0.8, il ne faut plus lire directement subject via ctx.auth.getUserIdentity(). Le guide recommande d’utiliser l’API du composant :

const userId = (await authComponent.getAuthUser(ctx))?.userId;

au lieu de :

const userId = (await ctx.auth.getUserIdentity())?.subject;

[Migrate to 0.8 – getUserIdentity]

Si vous me montrez l’import actuel de getUserId, je peux préciser exactement laquelle de ces options vous devez utiliser.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@heady ocean Kapa.ai is still learning and improving, please let me know how I did by reacting below.

heady ocean
#

avec convex auth @frail glacier export const generateAvatarUploadUrl = mutation({
args: { organizationId: v.id("organizations") },
handler: async (ctx, { organizationId }) => {
const userId = await auth.getUserId(ctx);
if (!userId) {
throw new Error("Non autorisé");
}

frail glacierBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Avec Convex Auth (@convex-dev/auth), le remplaçant de auth.getUserId(ctx) est getAuthUserId(ctx) depuis @convex-dev/auth/server. [getAuthUserId; Use auth state]

Adapte ta mutation ainsi :

import { mutation } from "./_generated/server";
import { getAuthUserId } from "@convex-dev/auth/server";

export const generateAvatarUploadUrl = mutation({
  args: { organizationId: v.id("organizations") },
  handler: async (ctx, { organizationId }) => {
    const userId = await getAuthUserId(ctx);
    if (userId === null) {
      throw new Error("Non autorisé");
    }

    // ... reste de ta logique
  },
});

getAuthUserId(ctx) renvoie Id<"users"> | null au lieu de la version dépréciée.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: