Back

account.get() does not return userId?

  • 0
  • Databases
  • React Native
  • Cloud
Quan
19 Mar, 2025, 09:15

after reading what i could find on sessions (https://appwrite.io/docs/references/cloud/models/session) and account (https://appwrite.io/docs/references/cloud/client-react-native/accountI), i seem to understand that:

  1. $id is the session id
  2. the userId is the user id... however, when i run account.get() i seem to get the session id for both of those. there are no id's that match in my database atleast,

i have it setup like this:

TypeScript
client
    .setEndpoint(config.endpoint)
    .setProject(config.projectId)
    .setPlatform(config.platform);

const account = new Account(client);

export async function getCurrentUser() {
    try {
        const user = await account.get();
        console.log('Current user fetched:', user);
        return user; 
    } catch (error) {
        console.log('No active session:', error);
        return null;
    }
}

this returns something like this:

TypeScript
Current user: {"$createdAt": "2025-03-18T07:10:17.842+00:00", "$id": "[redacted]dc362c", "$updatedAt": "2025
-03-19T07:14:11.225+00:00", "accessedAt": "2025-03-19T07:14:11.220+00:00", "email": "quan@example.com", "emailVerification": tr
ue, "labels": ["admin"], "mfa": false, "name": "quan", "passwordUpdate": "2025-03-18T07:10:17.839+00:00", "phone": "", "phoneVe
rification": false, "prefs": {}, "registration": "2025-03-18T07:10:17.839+00:00", "status": true, "targets": [{"$createdAt": "202
5-03-18T07:10:17.896+00:00", "$id": "[redacted]5ebed2", "$updatedAt": "2025-03-18T07:10:17.896+00:00", "expired": false, "ide
ntifier": "junker@example.com", "name": "", "providerId": null, "providerType": "email", "userId": "[redacted]dc362c"}]}

the user id that i can see in the database (or collection) is "[redacted]2eefbb"

am i understanding the documentation wrong? am i reading the returned data wrong perhaps? i dont quite understand what is at work here, as it seem other users on the internet are just getting their id'd like they want.

any help is appreciated

TL;DR
Developers are experiencing an issue where `account.get()` is returning a session id instead of the user id. The user id retrieved from the document does not match the one from the authentication. The problem lies in misunderstanding the returned data. Solution: When using `account.get()`, the session id is returned as `$id`, while the user id can be found in one of the objects inside the `targets` array. Developers should look for the user id under the `userId` key within the `targets` object.
Quan
19 Mar, 2025, 09:49

it seems like i have indeed misunderstood. what is happening is that i am getting the user id from a schema or document, which is the same user as the one in auth. however, the id's arent the same, and i am ofcourse checking up on the auth id, against the other id, which would never be the same

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more