Back

401 error

  • 0
  • Web
REXTONN
9 Feb, 2024, 17:50

I am facing this issue in my project and I am totally blank as to what to do exactly. Please can you guide me because I am quite new to this things. Thanks

TL;DR
Developers are experiencing a 401 error related to login/signup buttons not working properly. They need to check request headers, ensure correct routing, and avoid setting API keys on the client side. A solution involves checking if onClick events are occurring, verifying routing, and obtaining the current user when logged in. When using Appwrite for backend services, developers need to verify their code for any errors utilizing the account, login, createAccount, getCurrentUser, or logout functions.
REXTONN
9 Feb, 2024, 18:20

I'll show you my code for login/signup.

// start with appwrite auth service 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)
    // .setKey(conf.appwriteApiKey)
    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 service :: getCurrentUser() :: ", error);
    }
    return null
}
async logout() {
    try {
        await this.account.deleteSessions()
    } catch (error) {
        console.log("Appwrite service :: logout() :: ", error);
    }
}
// async updateUserRoles(userId, role, scope) {
//     try {
//         // Use the appropriate method from the Appwrite SDK to update user roles
//         await this.account.updateUserRoles(userId, role = "guests", scope = "account");
//     } catch (error) {
//         console.log("Error updating user roles:", error);
//         throw error;
//     }
// }

}

const authService = new AuthService()

export default authService

D5
9 Feb, 2024, 19:10

If this is client side, don't set api key

D5
9 Feb, 2024, 19:11

The code looks correct. You're not getting the current user because it's logged out

Steven
9 Feb, 2024, 19:39

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

REXTONN
10 Feb, 2024, 08:52

I did try to login through my interface but my login signup buttons aren't responding because of this error

D5
10 Feb, 2024, 16:46

What's te logic performed once clicked the button?

Hashira
10 Feb, 2024, 21:07

You should check if the onClick event is occurring or not . Also make sure that the routing is correct since I missed Outlet in my App.jsx Once you are Signed and logged in you will get Current User

Hashira
11 Feb, 2024, 07:28

Just an Update Check for CORS error and add your platform in your project

REXTONN
11 Feb, 2024, 08:07

i did add the cors and added the platform

Steven
11 Feb, 2024, 16:39

Please share the request headers for the failed request

REXTONN
22 Feb, 2024, 07:08

what do you mean by request headers?

REXTONN
22 Feb, 2024, 07:57

The problem which I am facing is that my Login/signup buttons are not working properly, so that's why I am not able to enter any users and thats the reason why I am getting this error.

REXTONN
22 Feb, 2024, 07:58

But the now I am not able to fix my bug.

Steven
22 Feb, 2024, 17:18

switch to the network tab in the browser dev tools and look for the failed request. when you look at the details, you should be able to see the request headers

REXTONN
22 Feb, 2024, 17:56

I can't see anything in my network tab

REXTONN
22 Feb, 2024, 17:56

It's all empty in there

REXTONN
22 Feb, 2024, 17:56
Steven
22 Feb, 2024, 17:57

refresh the page and run through the process again

REXTONN
22 Feb, 2024, 17:58

yes now I can see

REXTONN
22 Feb, 2024, 17:58

I'll send it right away

REXTONN
22 Feb, 2024, 17:59
REXTONN
22 Feb, 2024, 17:59
Steven
22 Feb, 2024, 18:00

so is this before logging in?

REXTONN
22 Feb, 2024, 18:00

yes

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