Skip to content
Back

GET https://cloud.appwrite.io/v1/account 401 (Unauthorized)

  • 0
  • Auth
  • Web
  • Webhooks
Eleven
4 Jun, 2024, 14:46

auth.js:

TypeScript
import conf from '../conf/conf.js';
import { Client, Account, ID } from "appwrite";


export class AuthService {
    client = new Client();
    account;

    constructor() {
        this.client
            .setEndpoint(conf.appwriteUrl)
            .setProject(conf.appwriteProjectId);
        this.account = new Account(this.client);

    }

    async createAccount({ email, password, name }) {
        try {
            const userAccount = await this.account.create(ID.unique(), email, password, name);
            if (userAccount) {
                
                return this.login({ email, password });
            } else {
                return userAccount;
            }
        } catch (error) {
            throw error;
        }
    }

    async login({ email, password }) {
        try {
            return await this.account.createEmailSession(email, password);
        } catch (error) {
            throw error;
        }
    }

    async getCurrentUser() {
        try {
            return await this.account.get();
        } catch (error) {
            console.log("Appwrite serive :: getCurrentUser :: error", error);
        }

        return null;
    }

    async logout() {

        try {
            await this.account.deleteSessions();
        } catch (error) {
            console.log("Appwrite serive :: logout :: error", error);
        }
    }
}

const authService = new AuthService();

export default authService

.env:

TypeScript
VITE_APPWRITE_URL=https://cloud.appwrite.io/v1
VITE_APPWRITE_PROJECT_ID="6**************4"
VITE_APPWRITE_DATABASE_ID='6*************8'
VITE_APPWRITE_COLLECTION_ID='6***********d'
VITE_APPWRITE_BUCKET_ID='6****************d'

I have attached the screenshot of the conf,js and logs, please help me to fix this issue

TL;DR
Developers are getting a 401 Unauthorized error when trying to access the account details in the Appwrite API. The issue is likely with the authentication process. The provided code shows the AuthService class with methods for creating an account, logging in, getting the current user, and logging out. The .env file contains the necessary environment variables. Check the authentication flow and ensure the credentials are correct.
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