Back

[SOLVED] Error getting the current logged user

  • 0
  • Users
  • Web
abel_banze
26 Dec, 2023, 11:01

I'm struggling the get the current logged user using the method Account.get().

my code:

TypeScript
export async function getCurrentUser(){
    try{

        const currentUser = await account.get();

        if(!currentUser) return null;

        const user = await databases.listDocuments(
            appwriteConfig.database,
            appwriteConfig.usersDB,
            [
                Query.equal('accountId', currentUser.$id)
            ]
        );

        if(!user) throw Error("Usuario logado nao encontrado.");

        return user.documents[0];

    }catch(error){
        console.log(error)
        throw Error("Erro ao carregar o usuario logado.")
    }
}

Its always returning the error:

TypeScript
code: 401,
  type: 'general_unauthorized_scope',
  response: {
    message: 'User (role: guests) missing scope (account)',
    code: 401,
    type: 'general_unauthorized_scope',
    version: '0.12.21'
  }

How do I solve this?

TL;DR
The user was experiencing an error when trying to get the currently logged user using the appwrite method `Account.get()`. They were getting a 401 error with the message "User (role: guests) missing scope (account)". The solution to the problem was that the code was being run on the server-side instead of the client-side. The user had to make sure the code is running on the client-side. Additionally, they needed to properly instantiate the appwrite client for the code to work correctly.
sswastik02
26 Dec, 2023, 11:24

Is this client-side code ? If yes, how have you instantiated the appwrite client ? Can you show please ?

abel_banze
26 Dec, 2023, 11:51

Opps! I was running the code on server-side. Thats was the error. thanks for your help

abel_banze
26 Dec, 2023, 11:52

[SOLVED] Error getting the current logged user

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