Back

Appwrite URL error in React Blog Project

  • 0
  • Web
Tiru99
30 Mar, 2024, 03:45

Hey guys I am working on a react project with appwrite backend services on a blog website, but I am constantly receiveing the error " Failed to load resource: the server responded with a status of 401 ()" , I also tried configuring cors , in the platforms section in appwrite but still I am getting the same error, can anyone please help me solve this problem , just let me know if you need any more details to solve the error .

TL;DR
Developers are encountering a 401 error in a React blog project using Appwrite. They are implementing a login method but facing issues. A suggestion is made to check the code against the Appwrite docs for session creation. The error might indicate a session not being created. CORS was also configured in Appwrite but the error persists. Further details are needed to troubleshoot.
Steven
30 Mar, 2024, 03:47

Did you create a session?

Tiru99
30 Mar, 2024, 03:55

How can I check that ?

Steven
30 Mar, 2024, 03:57

Look at your code and compare to the docs:

https://appwrite.io/docs/products/auth/quick-start#login

Tiru99
30 Mar, 2024, 03:58

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

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

TypeScript
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.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

this is my code, and the login method is the session method I implemented , can you please have a look at this

Steven
30 Mar, 2024, 03:59

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

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