Back

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

  • 0
  • Auth
  • Cloud
Jayant
28 Jun, 2024, 17:34

Please help me to solve this error

TL;DR
Developers are getting a 401 Unauthorized error when trying to make a GET request to https://cloud.appwrite.io/v1/account. It seems like an authentication issue. Make sure the authentication process is correctly set up in the AuthService class. Also, check if the credentials are being passed correctly during login.
Jayant
28 Jun, 2024, 17:35
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) {
                // call another method
                return this.login({email, password});
            } else {
               return  userAccount;
            }
        } catch (error) {
            throw error;
        }
    }

    async login({email, password}) {
        try {
            return await this.account.createEmailPasswordSession(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
Jayant
28 Jun, 2024, 17:37

this is the error

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