#How can i add labels to users in auth, using User ID?
181 messages · Page 1 of 1 (latest)
I have looked at https://appwrite.io/docs/products/auth/labels but cant solve it anyways
(HELP) How can i add labels to users in auth, using User ID?
How can i add labels to users in auth, using User ID?
what do you mean it didn't work?
this is also databases.updateDocument(). not users.updateLabels() like the docs mention
whne asking for help, it's best to be detailed rather than just saying "it didn't work"
I have this error when trying to use the users.updateLabels @peak karma
Or must i use the sdk and some api?
Alright, how would you build the export function? appreciate the help!
This correct then?
I made a new sdk.ts like this and it worked once but is it any cooldowns or stuff with appwrite? Sometimes it don't add the label
im not sure what you mean by cooldowns
please make sure you are not adding this code client side. an api key should NEVER be used client side because it exposes the api key
so in your case, anyone can give themselves that premium label or remove labels or fetch everyone's user data
yeah i understand, i have not used 'use client' in any of it
idk but i tryed to subscribe 10 times and 2 times i got the label
you're using NEXT_PUBLIC_ so that risks leaking it client side
Okey, but how can i extract it from .env.local otherwise?
try adding some logging to try to see what's going on
This is my updateUserLabels.ts according to the docs and i get the message that it works, but the labels in auth is not updating. Any clue? tnx
(all the client info is correct)
Why cant i just add "premium"?(admin and mvp works)
Wdym? Premium works for me
Its so strange and glitchy. Sometimes it updates label and sometimes it just keeps the old one
if i totaly change the label and rerun it, it updates. If u just change 1 character or some it just keeps the old one
and i never get the premium label to apply
only "PREMIUM"
Can you try manually via Postman
Can u see if i made any misstakes?
and yes "Premium" works but not "premium"
What if you do it in the console?
It works for me in all cases and in the database it is also stored correctly.
i only use Auth atm, removed it from db. Idk this error is so weird
The response also says that it has uppdated correctly and that the label is "premium" but appwrite auth is not uppdated
Can you please try it out manually first?
So we can at least single some causes out
With Postman?
Console or/and Postman
It says in the response that labels is premium
and its localhost
What did you screenshot?
the server side console
So it works as expected? But not if you do that via your frontend?
the console works yes but appwrite don't update
that is the weird part, and i have zero errors
The Label in Auth dont update sometimes, even if the console says that is has updated
The console has always sad that is worked and the label updated to the new one, but in Auth label it only update sometimes
How do you determine that it has not updated?
I refresh appwrite page and se if the new label is on the profile
appwrite page = appwrite console auth tab?
I am on appwrite-auth and on my profile and looking at the label section
I refresh this side, and only sometimes it has been updated
Chrome
And that only happens to premium in lowercase?
i have never got "premium" to be applied. And it never works if you change the label to something similar as you had before, example "Sub" to "Subscription"
it only works then i change label to something totaly diffrent
and in the server console it always says that it worked and the new label is applied, but in reality, its not always applied
its so unpredictable and that is why i cant figure it out
it is just like appwrite is glitching randomly
Please provide a step by step guide where you are certain that this always happens
In bulletpoints
Because what you described sounds really weird
yeah i know
this is my return/page.tsx either way.
import { stripe } from "../utils/stripe";
import { updateUserLabels } from "@/lib/updateUserLabels";
interface SearchParams {
session_id: string;
}
async function getSession(sessionId: string) {
const session = await stripe.checkout.sessions.retrieve(sessionId);
return session;
}
export default async function CheckoutReturn({ searchParams }: { searchParams: SearchParams }) {
const sessionId = searchParams.session_id;
const session = await getSession(sessionId);
const userId = session.client_reference_id;
console.log("Retrieved session:", session);
if (session?.payment_status === "paid" && userId) {
console.log("Payment is successful, updating user labels...");
await updateUserLabels(userId, 'premium');
} else {
console.log("Payment is not successful or user ID is missing.");
}
return (
<div className="flex items-center justify-center min-h-screen">
<div className="bg-white p-6 rounded-lg shadow-md w-full max-w-md text-center">
{session?.payment_status === "unpaid" ? (
<div>
<h2 className="text-2xl font-bold mb-4">Payment Failed</h2>
<p className="text-red-500">Payment did not work. Please try again.</p>
</div>
) : session?.payment_status === "paid" ? (
<div>
<h2 className="text-2xl font-bold mb-4">Thank You!</h2>
<p className="text-green-500">We appreciate your subscription!</p>
<p>Your Stripe customer ID is: <span className="font-mono">{session.customer as string}</span></p>
<p>Your User ID is: <span className="font-mono">{session.client_reference_id}</span></p>
</div>
) : (
<div>
<h2 className="text-2xl font-bold mb-4">Processing Payment</h2>
<p>Please wait while we process your payment.</p>
</div>
)}
</div>
</div>
);
}
and updateUserLabels.ts
Can you show me how you have set up the structure? I give up on appwrite soon, the old database was so stright forward
Really appreciate the effort and support anyways
In all honesty this is a really weird issue and I can't pinpoint where the issue is. Your code does not say say much nor does it look wrong on first glance. You have to provide as much information as possible in order to make debugging easier. I can't reproduce this on my end so you have to provide a step for step guide when this error happens.
What do you mean Appwrite don't update? So you:
- Went to the user in the Appwrite console
- Updated labels to be premium
- Clicked update
- Refreshed the page and...
No i run the code to add label to the user i min auth. And after my server side console says that it worked, i go to appwrite and refresh the page, and sometimes there is no label added to the user/or the old label is still there.
yeah i understand, sry that i cant provide more information
So to clarify. This only happens when you run "your" code?
The one you sent in the chat?
Now i got something intressting, look
This is my console in vscode
And now i tryed something completly diffrent. And the label is uppdated
And now i try "premium" but it has not uppdated in appwrite
- i dont even have admin label
maby the user has old labels that i cant see on appwrite website? is there any way to print all labels added to user?
Can you please log the response from users.get as well please
It seems like that was the problem, i can see admin and premium label already
but i cant see it on appwrite website
can i clear all labels somehow?
lmao this is some weird shit
what sdk and skd version is this? let me check the source code of it real quick
do updateLabels append the new label or removes all old and add one new?
Drop all the complexity and start a new project and just execute that flow without anything else
It replaces the previous labels.
Like with all arrays in appwrite. In documents or user prefs
It always does a full update
alright, idk why the admin label is there then
but this process works for you with appwrite?
Yes
There should not be admin in the first place when you never gave the user that value
Can you please answer my question
yeah me neither. Will try the process with another account and se if admin label still is there, ik that on this account i have had admin label before
Alright, it works with premium on another account, where i got no labels before
can it has anything to do with my other appwrite.ts file where i createUsers and do all the other stuff? idk all have to be sdk or something
because i saw in the documentation create user with the sdk, but i dont use sdk when i create my users
I understand but do you know if i have to always use sdk?
Now i found something that could be the issue. When i log await users.get(userId) and have added "admin and mvp" on the website to the user, it still says in the console that the user dont have any labels, but the user information is correct. just like there is dual users with just diffrent labels. And i see one in appwrite and update one with the sdk.
I made this test.
and this is the response in console.
just like the labels don't apply
and on the appwrite website i now got "admin" label, after all the code was compiled
just like something with appwrite is glitching
Right after you run this, please share a screenshot of the user in the appwrite console
Please share a screenshot of the full window, including the url in the browser address bar
Before you call the update labels, please also log users.client.config.project
Now i runned this.
Sometimes it updates the label to premium and sometimes not, but it always says that it successfully updated
this is the return/page.tsx part
And it always return succcess
this is the appwrite console url
Open the user and share the URL of that page
Do you have any call to update labels anywhere else?
No i only use it once
Would you please open the browser dev tools and use the network inspector to look for the network request for the user and share the response
Do you mean this? @peak karma
I meant in the Appwrite console
How can i find browser dev tools in appwrite console? Or do you mean Vercel logs where i host it?
The browser dev tools are on the right in this screenshot
Yes, this is the Appwrite Console
So what did you mean with this? don't understand what i need to send
where can i find the "network request for the user and share the response"
Browse to the page of the user, open the dev tools, refresh the page, and look for the network request that returns the user
the page don't fetch the user, it sends userId threw session
from the subscription page, to return page
Go to the appwrite console page for the user
This one
Yes, and now?
Did you do the rest of this?
There should be a fetch call that returns the user as JSON
This one?
Does the response look like JSON for that?
It would help to filter for fetch/xhr requests
That is some JavaScript code, but that is not JSON of the user
this "account" response then?
This is JSON, but it is not the user
This user
Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting
K now go through the flow in your app to update the labels to premium and then refresh the page in the Appwrite Console so you get this data again
Yes it seems to work now, have done it 4 times.
But it has been working sometimes before and all suddenly it just stops to work
4 in a row is record anyways
And now i only worked 1 time on the macbook, the 2th time it didnt update.
Did you set it to admin before running the test?
Yes i always change back to admin before