Skip to content
Back

account.get() fails all the time

  • 0
  • Auth
  • Web
Los Feliz
25 Mar, 2025, 20:40

I have added a Google OAuth2 login to my reactjs app. It successfully login the user. When the user logs in, I see that their activity is registered in the Users tab of the Auth section. I also see the session.create event for the logged in user in the Activity tab. However, I get the Unauthorized error when I run account.get(). I do not understand how.

db.js:

TypeScript
import { Client, Storage, Account, Databases, ID, Query, Permission, Role, Functions, OAuthProvider } from 'appwrite';

const client = new Client()
    .setEndpoint(import.meta.env.VITE_ENDPOINT)
    .setProject(import.meta.env.VITE_PROJECT);

export const account = new Account(client);
const functions = new Functions(client);
export default client;
const storage = new Storage(client);
export const databases = new Databases(client);

export const googleOAuthLogin = async () => {
    try {
        const userAcc = account.createOAuth2Session(
            OAuthProvider.Google, 
            'http://localhost:5173/user/me',
            'http://localhost:5173/',
        ); 
        console.log('Success:', userAcc); 
    } catch (error) {
        console.error('Error:', error);
    }
}
 
export const getAccount = async () => {
    try {
        const accnt = await account.get();
        console.log('Account gotten successfully:', accnt);
        return accnt;
    } catch (err) {
        console.error('Error:', err);
    }
}

Me.jsx:

TypeScript
import React, { useEffect } from 'react';
import { getAccount } from '../db';

const Me = () => {
    useEffect(() => {
        const testingGetAccount = async () => {
           try {
                const accnt = await getAccount();
                console.log('THIS IS ACCOUNT:', accnt);
            } catch (err) {
                console.error('Error', err); }
}
    testingGetAccount();
    }, []);
} 

GET https://cloud.appwrite.io/v1/account 401 (Unauthorized) Error getting account: AppwriteException: User (role: guests) missing scope (account)

TL;DR
Developers are facing an issue where `account.get()` is returning an Unauthorized error even though the user is successfully logged in with Google OAuth2. The error indicates that the user role is missing the required scope for account access. To resolve this, developers need to ensure that the user role has the necessary permission to access the account scope.
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